找不到页面 (404) 请求方法:GET 请求 URL: http://127.0.0.1:8000/blog/blog/Blog%20comments/

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/blog/blog/Blog%20comments/

博客应用程序 urls

url模式 = [

path('', views.blog, name="blog"),
path('postComment/', views.postComment, name="postComment"),
path('<str:slug>/', views.blogPage, name="blogPage"),

]+静态(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

项目urls/

url模式 = [

path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),

]

家庭应用程序 urls/

url模式 = [

path('', views.index, name="index"),
path('contact/', views.contact, name="contact"),
path('about/', views.about, name="about"),

] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

blog.html/

               <a href="blog/{{post.slug}}">{{post.title}}</a>

之后url变成了http://127.0.0.1:8000/blog/blog/Blog%20comments/

blog.html(已更改)/

                <a href="/{{post.slug}}">{{post.title}}</a>

之后 url 变成了 http://127.0.0.1:8000/Blog%20comments/

在这两种情况下都找不到页面

但是从主页 url 变成了这个并且工作 http://127.0.0.1:8000/blog/Blog%20comments/

index.html/

               <a href="blog/{{post.slug}}">{{post.title}}</a>

blog/views.py/

def 博客(请求):

allPosts= Post.objects.all()
context={'allPosts': allPosts}
return render(request, "blog/blog.html", context)

def blogPage(请求,别名):

post=Post.objects.filter(slug=slug).first()
comments= BlogComment.objects.filter(post=post, parent=None)
context={"post":post, 'comments': comments,}
return render(request, "blog/blogPage.html", context)

您使用的 url 配置有误。只需在您的 html 中使用它来渲染您的 url:

<a href="{% url 'blogPage' post.slug %}">{{post.title}}</a>

不要在 href 中使用任何 /blog/