gpt4 book ai didi

python - 将 url 正则表达式转换为 django 2.1 中的路径

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:18 27 4
gpt4 key购买 nike

url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),

我一直在做教程,但在 django 2.1 中你必须使用路径,我如何转换为 2.1 django compatibile 路径函数?

是否

path('activate/<str:uidb64>/<uuid:token>/', views.activate, name='activate')

做同样的事吗?

最佳答案

I've been doing tutorial but in django 2.1 you have to use path, how do I translate to 2.1 django compatibile path function?

,在 , 你可以使用 path [Django-doc]re_path [Django-doc] .此外,截至今天,url [Django-doc]仍受支持,但将来可能会消失。

re_path 实际上等同于旧的 url,所以你可以这样写:

<b>re_path</b>(
r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate,
name='activate'
),

构造一个完全等价的 URL 并不容易,因为 Django 只支持 five path conversions by default :

Path converters

The following path converters are available by default:

  1. str - Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn't included in the expression.
  2. int - Matches zero or any positive integer. Returns an int.
  3. slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
  4. uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
  5. path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.

我们可以在这里使用 slug,但这将比给定的 URL 匹配更多:

<b>path</b>(
r'^activate/(<<b>slug:</b>uidb64>/<<b>slug:</b>token>/$',
views.activate,
name='activate'
),

slug pattern takes as regex equivalent :

class SlugConverter(StringConverter):
regex = '[-a-zA-Z0-9_]+'

关于python - 将 url 正则表达式转换为 django 2.1 中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53173201/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com