博客應用網址
網址模式 = [
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)
專案地址/
網址模式 = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),
]
主頁應用程式網址/
網址模式 = [
path('', views.index, name="index"),
path('contact/', views.contact, name="contact"),
path('about/', views.about, name="about"),
] 靜態(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
博客.html/
<a href="blog/{{post.slug}}">{{post.title}}</a>
在這個 url 變成 http://127.0.0.1:8000/blog/blog/Blog comments/之后
blog.html(已更改)/
<a href="/{{post.slug}}">{{post.title}}</a>
在這個網址變成http://127.0.0.1:8000/Blog comments/之后
在這兩種情況下都找不到頁面
但是從主頁 url 變成了這個并且可以作業 http://127.0.0.1:8000/blog/Blog comments/
索引.html/
<a href="blog/{{post.slug}}">{{post.title}}</a>
博客/views.py/
定義博客(請求):
allPosts= Post.objects.all()
context={'allPosts': allPosts}
return render(request, "blog/blog.html", context)
def blogPage(請求,slug):
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)
uj5u.com熱心網友回復:
您使用了錯誤的 url 配置。只需在您的 html 中使用它來呈現您的網址:
<a href="{% url 'blogPage' post.slug %}">{{post.title}}</a>
不要使用任何/或blog/內部的href。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424950.html
