Slug 字段后跟 url
我刚刚开始使用 Django 和 Python,所以我对此还很陌生。 这是我的 urls.py:
url(r'(?P<slug>[-\w]+)/$','person_detail'),
url(r'(?P<slug>[-\w]+)/delete/$','person_delete'),
问题是,当我尝试对 url: slug/delete/ 执行操作时,它正在寻找整个部分 slug/delete/ 作为 slug。当我删除第一个网址中的 $ 时,它不会转到 person_delete 视图,而是转到 person_detail 视图,忽略 /delete/ 部分 有什么想法吗?
I just started Django and Python, so Im still new to this..
This is my urls.py:
url(r'(?P<slug>[-\w]+)/
The problem is that when I try to do to the url: slug/delete/ it's looking for that whole part slug/delete/ as the slug. When i remove the $ in the 1st url it does not go to the person_delete view, but goes to the person_detail view, ignoring the /delete/ part
Any ideas?
,'person_detail'),
url(r'(?P<slug>[-\w]+)/delete/
The problem is that when I try to do to the url: slug/delete/ it's looking for that whole part slug/delete/ as the slug. When i remove the $ in the 1st url it does not go to the person_delete view, but goes to the person_detail view, ignoring the /delete/ part
Any ideas?
,'person_delete'),
The problem is that when I try to do to the url: slug/delete/ it's looking for that whole part slug/delete/ as the slug. When i remove the $ in the 1st url it does not go to the person_delete view, but goes to the person_detail view, ignoring the /delete/ part
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试添加前导
^
:也就是说,如果没有前导
^
,我希望foo/delete/
让您到达person_detail
视图中的slug
为delete
,而不是foo/delete
。Try adding a leading
^
:That said, without the leading
^
I'd expectfoo/delete/
to get you to theperson_detail
view withslug
asdelete
, rather thanfoo/delete
.怎么样
确保 slug 不能包含斜杠?您还可以尝试以相反的顺序使用规则,尝试让 Django 首先匹配 /.../delete/ 。
How about something like
to make sure the slug can not contain a slash? You could also try having the rules in the opposite order, to try have Django match /.../delete/ first.
请注意,slug 字段还可能包含数字(不仅仅是字母和破折号),因此您需要将其更改为:
Note that slug fields might also include digits (not just letters and the dash), so you want to alter it to say something like:
在这种情况下,URL 顺序很重要,因为 url 调度程序使用第一个匹配。通用网址应该放在最后。
Url order is important in such cases, because url dispacher using first match. Common url should be last.