Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Url,详细信息页面的Slug_Django_Slug_Urlconf - Fatal编程技术网

Django Url,详细信息页面的Slug

Django Url,详细信息页面的Slug,django,slug,urlconf,Django,Slug,Urlconf,配置url以显示详细视图时遇到问题。点击这个链接:显示blog.html,当时我以为它会显示blog detail.html。没有错误,浏览器栏显示:example.com/blog/the slug,但仍然显示blog.html,而不是blog detail.html。你知道为什么吗?谢谢你的想法 网址: 编辑:@omouse请求的输出 这是单击链接的输出。它与blog.html完全相同,但应该是blog detail.html <div id='content-wrapper'>

配置url以显示详细视图时遇到问题。点击这个链接:
显示
blog.html
,当时我以为它会显示
blog detail.html
。没有错误,浏览器栏显示:
example.com/blog/the slug
,但仍然显示
blog.html
,而不是
blog detail.html
。你知道为什么吗?谢谢你的想法

网址:


编辑:@omouse请求的输出

这是单击链接的输出。它与
blog.html
完全相同,但应该是
blog detail.html

<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&#39;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&#39;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>

Lorem Ipsum只是印刷和排版行业的虚拟文本。Lorem Ipsum一直是该行业的佼佼者';从16世纪开始,一个不知名的印刷商拿起一个打印盘,把它拼凑成一个。。。
Lorem Ipsum只是印刷和排版行业的虚拟文本。Lorem Ipsum一直是该行业的佼佼者';从16世纪开始,一个不知名的印刷商拿起一个打印盘,把它拼凑成一个。。。

URL是问题所在,第一个URL将匹配所有内容(
/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'),
url(r'^blog/$,'myapp.views.blog',name='blog'),
url(r'^blog/(?P[\w-]+)/$,'myapp.views.blog_detail',name='blog_detail'),
上述方法应能正常工作


鲁道夫完全正确

/$
阻止blog捕获slug调用的所有子页面 因此,如果您有子页面,则需要将
/$
添加到文件夹级别,如下所示:

re_path('brands/$', AllBrands.as_view(), name="brands"),
re_path(r'^brands/(?P<slug>[\w-]+)/$', BrandDetail.as_view(), name = 'brandetail'),
re_path('brands/$,AllBrands.as_view(),name=“brands”),
re_路径(r'^brands/(?P[\w-]+)/$),BrandDetail.as_view(),name='BrandDetail'),
这是django 2.2


没有品牌之后的
/$
,slug页面将显示品牌列表页面。

试试这个是的,已经试过了,谢谢。还是不走运。不确定会出现什么问题…您可以添加模板输出吗?谢谢@omouse:模板输出是
blog.html
。它只是一个简单的虚拟文本,但我已经在上面包含了它。
url(r'^blog/$', 'myapp.views.blog', name='blog'),
url(r'^blog/(?P<slug>[\w-]+)/$', 'myapp.views.blog_detail', name='blog_detail'),
re_path('brands/$', AllBrands.as_view(), name="brands"),
re_path(r'^brands/(?P<slug>[\w-]+)/$', BrandDetail.as_view(), name = 'brandetail'),