简体   繁体   English

Django slug URL 中的正则表达式

[英]Regular expression in URL for Django slug

I have 2 URL's with a slug field in the URL.我有 2 个 URL,在 URL 中有一个 slug 字段。

url(r'^genres/(?P<slug>.+)/$', views.genre_view, name='genre_view'),
url(r'^genres/(?P<slug>.+)/monthly/$', views.genre_month, name='genre_month'),

The first one opens fine but the second one gives a DoesNotExist error saying Genres matching query does not exist .第一个打开正常,但第二个给出了一个DoesNotExist错误,说Genres matching query does not exist

Here is how I'm accessing the 2nd URL in my HTML这是我访问 HTML 中第二个 URL 的方式

<li><a href="{% url 'genre_month' slug=genre.slug %}">Monthly Top Songs</a></li>

I tried to print the slug in the view.我试图在视图中打印 slug。 It is passed as genre_name/monthly instead instead of genre_name .它被传递为genre_name/monthly而不是genre_name

I think the problem is with the regex in the URLs.我认为问题在于 URL 中的正则表达式。 Any idea what's wrong here?知道这里有什么问题吗?

Django always uses the first pattern that matches. Django 总是使用第一个匹配的模式。 For urls similar to genres/genre_name/monthly your first pattern matches, so the second one is never used.对于类似于genres/genre_name/monthly网址,您的第一个模式匹配,因此永远不会使用第二个模式。 The truth is the regex is not specific enough, allowing all characters - which doesn't seem to make sense.事实是正则表达式不够具体,允许所有字符 - 这似乎没有意义。

You could reverse the order of those patterns, but what you should do is to make them more specific (compare: urls.py example in generic class-based views docs ):您可以颠倒这些模式的顺序,但您应该做的是使它们更具体(比较: 基于类的通用视图文档中的 urls.py 示例):

url(r'^genres/(?P<slug>[-\w]+)/$', views.genre_view, name='genre_view'),
url(r'^genres/(?P<slug>[-\w]+)/monthly/$', views.genre_month, name='genre_month'),

Edit 2020: 2020年编辑:

Those days (since Django 2.0), you can (and should) use path instead of url .那些日子(自 Django 2.0 起),您可以(并且应该)使用path而不是url It provides built-in path converters , including slug :它提供了内置的路径转换器,包括slug

path('genres/<slug:slug>/', views.genre_view, name='genre_view'),
path('genres/<slug:slug>/monthly/', views.genre_month, name='genre_month'),

I believe that you can also drop the _ from the pattern that @Ludwik has suggested and revise to this version (which is one character simpler :) ):我相信你也可以从@Ludwik建议的模式中删除_并修改到这个版本(这是一个更简单的字符 :) ):

url(r'^genres/(?P<slug>[-\w]+)/$', views.genre_view, name='genre_view'),
url(r'^genres/(?P<slug>[-\w]+)/monthly/$', views.genre_month, name='genre_month'),

Note that \\w stands for " word character ".请注意, \\w代表“单词字符”。 It always matches the ASCII characters [A-Za-z0-9_] .它始终匹配 ASCII 字符[A-Za-z0-9_] Notice the inclusion of the underscore and digits.请注意包含下划线和数字。 more info更多信息

In Django >= 2.0, slug is included in URL by doing it like below.在 Django >= 2.0 中,slug 通过如下方式包含在 URL 中。

from django.urls import path

urlpatterns = [
    ...
    path('articles/<slug:some_title>/', myapp.views.blog_detail, name='blog_detail'),
    ...
]

Source: https://docs.djangoproject.com/en/2.0/ref/urls/#django.urls.path来源: https : //docs.djangoproject.com/en/2.0/ref/urls/#django.urls.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM