我无法配置我的网址以显示详细信息视图。点击此链接:<a href='{% url blog_detail blog.slug %}'>{{ blog.name }}</a>
会显示blog.html
,当我认为它会显示blog-detail.html
时。没有错误,浏览器栏显示:example.com/blog/the-slug
,但仍然显示来自blog.html
的html,而不是blog-detail.html
。有什么想法吗?谢谢你的想法。
URL:
url(r'^blog/', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),
的观点:
def blog(request):
blog_list = Blog.objects.all()
return render(request, 'blog.html', {'blog_list':blog_list})
def blog_detail(request, slug):
blog = get_object_or_404(Blog, slug=slug)
return render(request, 'blog-detail.html', {'blog':blog})
编辑:@omouse请求的输出
这是点击链接的输出。它与blog.html
完全相同,但应为blog-detail.html
。 Argggg!
<div id='content-wrapper'>
<section>
<div class='blog-name'><h2><a href='/blog/test/'>Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...
<div class='blog-name'><h2><a href='/blog/second-test/'>Second Test</a></h2></div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a ...
</section>
</div>
答案 0 :(得分:27)
网址是问题所在,第一个会匹配所有内容(/blog/
,/blog/test/
,/blog/awdlawdjaawld
),您需要在结尾处使用美元符号$
仅匹配/blog/
。
url(r'^blog/$', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),
以上应该可以正常工作。
答案 1 :(得分:0)
鲁道夫绝对正确
/$
阻止博客捕获slug调用的所有子页面
因此,如果您有子页面,则需要将/$
添加到文件夹级别,如下所示:
re_path('brands/$', AllBrands.as_view(), name="brands"),
re_path(r'^brands/(?P<slug>[\w-]+)/$', BrandDetail.as_view(), name = 'brandetail'),
这是django 2.2
在品牌之后没有/$
的情况下,子弹页显示的是品牌列表页。