Slug 字段后跟 url

发布于 2024-08-10 07:50:05 字数 346 浏览 2 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

够运 2024-08-17 07:50:05

尝试添加前导 ^

url(r'^(?P<slug>[-\w]+)/

也就是说,如果没有前导 ^,我希望 foo/delete/ 让您到达 person_detail 视图中的 slugdelete,而不是 foo/delete

,'person_detail'), url(r'^(?P<slug>[-\w]+)/delete/

也就是说,如果没有前导 ^,我希望 foo/delete/ 让您到达 person_detail 视图中的 slugdelete,而不是 foo/delete

,'person_delete'),

也就是说,如果没有前导 ^,我希望 foo/delete/ 让您到达 person_detail 视图中的 slugdelete,而不是 foo/delete

Try adding a leading ^:

url(r'^(?P<slug>[-\w]+)/

That said, without the leading ^ I'd expect foo/delete/ to get you to the person_detail view with slug as delete, rather than foo/delete.

,'person_detail'), url(r'^(?P<slug>[-\w]+)/delete/

That said, without the leading ^ I'd expect foo/delete/ to get you to the person_detail view with slug as delete, rather than foo/delete.

,'person_delete'),

That said, without the leading ^ I'd expect foo/delete/ to get you to the person_detail view with slug as delete, rather than foo/delete.

最美不过初阳 2024-08-17 07:50:05

怎么样

url(r'(?P<slug>[^/]+)/

确保 slug 不能包含斜杠?您还可以尝试以相反的顺序使用规则,尝试让 Django 首先匹配 /.../delete/ 。

,'person_detail'), url(r'(?P<slug>[^/]+)/delete/

确保 slug 不能包含斜杠?您还可以尝试以相反的顺序使用规则,尝试让 Django 首先匹配 /.../delete/ 。

,'person_delete'),

确保 slug 不能包含斜杠?您还可以尝试以相反的顺序使用规则,尝试让 Django 首先匹配 /.../delete/ 。

How about something like

url(r'(?P<slug>[^/]+)/

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.

,'person_detail'), url(r'(?P<slug>[^/]+)/delete/

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.

,'person_delete'),

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.

深海不蓝 2024-08-17 07:50:05

请注意,slug 字段还可能包含数字(不仅仅是字母和破折号),因此您需要将其更改为:

SLUG = '(?P<slug>[\w\d-]+)'

url(r'^'+SLUG+'/delete
, delete_method, {}, 'delete_url_name')

Note that slug fields might also include digits (not just letters and the dash), so you want to alter it to say something like:

SLUG = '(?P<slug>[\w\d-]+)'

url(r'^'+SLUG+'/delete
, delete_method, {}, 'delete_url_name')
等你爱我 2024-08-17 07:50:05
url(r'(?P<slug>[-\w]+)/delete/

在这种情况下,URL 顺序很重要,因为 url 调度程序使用第一个匹配。通用网址应该放在最后。

,'person_delete'), url(r'(?P<slug>[-\w]+)/','person_detail'),

在这种情况下,URL 顺序很重要,因为 url 调度程序使用第一个匹配。通用网址应该放在最后。

url(r'(?P<slug>[-\w]+)/delete/

Url order is important in such cases, because url dispacher using first match. Common url should be last.

,'person_delete'), url(r'(?P<slug>[-\w]+)/','person_detail'),

Url order is important in such cases, because url dispacher using first match. Common url should be last.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文