Whats the correct way to use and refer to a slugfield in a django 1.3

后端 未结 4 880
日久生厌
日久生厌 2021-02-04 22:33

Whats the correct way to use and refer to a slugfield in a django 1.3

for example the following code should link via slug to a generic view however the NoReverseMatch er

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 22:43

    In Django 1.5 the slug validator uses this regex:

    slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
    

    See https://github.com/django/django/blob/stable/1.5.x/django/core/validators.py#L106

    You can use this regex in urls.py:

    url(r'^post/(?P[-a-zA-Z0-9_]+)/$', ...
    

    In earlier versions it was [-\w]+ but I guess in Python3 \w matches non ascii characters like umlauts.

提交回复
热议问题