Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

python - Page not found (404) Request Method: GET ....... the current path, blog/blogpost, didn't match any of these

Hi i am trying to make blog website but while i fetch model with blogpost function in views.py its shows error that 404 page not found like ||Using the URLconf defined in mac.urls, Django tried these URL patterns, in this order: admin/ shop/ blog/ [name='BlogHome'] blog The current path, blog/blogpost, didn't match any of these. ||

-until i don't create model it works fine but after creating model and trying to fetch articles through post_id it throws error as page not found! -what am i missing? -Here is the codes i am using.

code of blog/urls.py ->

    from django.urls import path
from . import views

urlpatterns = [
    path(" ", views.index, name="ShopHome"),
    path("blogpost/<int:id>/", views.blogpost, name="blogpost")]
    

code of blog/Views.py ->

from django.shortcuts import render
from .models import Blog

# Create your views here.
from django.http import HttpResponse

def index(request):
        return render(request, 'blog/index.html')

def blogpost(request,id):
    post= Blog.objects.filter (post_id=id)[0] 
    print(post)
    return render(request, 'blog/blogpost.html',{'post':post})

code of blog/adminpy ->

from django.contrib import admin

from .models import Blog


admin.site.register(Blog)

code of blog/models.py ->

from django.db import models

# Create your models here.
class Blog(models.Model):
    post_id = models.AutoField(primary_key= True)
    title = models.CharField(max_length=50)
    title0 = models.CharField(max_length=500,default='')
    title1= models.CharField(max_length=500,default='')
    title2= models.CharField(max_length=500,default='')
    Content_title= models.CharField(max_length=500,default='')
    Content_title0= models.CharField(max_length=500,default='')
    Content_title1= models.CharField(max_length=500,default='')
    Content_title2= models.CharField(max_length=500,default='')
    pub_date = models.DateField()
    image = models.ImageField(upload_to='shop/images', default="")

    def __str__(self):
        return self.title
question from:https://stackoverflow.com/questions/65845223/page-not-found-404-request-method-get-the-current-path-blog-blogpost

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are passing an /<int:id>/ (btw always include the last trailing slash in your urls /) but your path says /blog/blogpost with a str parameter where it should be an int representing the id of an existing Blogpost instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...