将 url 正则表达式翻译成 Django 2.1 中的路径
Translating url regular expressions into path in django 2.1
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?
否,在django-2.x, you can use path
[Django-doc] or re_path
[Django-doc]. Furthermore as of today, 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:
str
- Matches any non-empty string, excluding the path
separator, '/'
. This is the default if a converter isn't included in the expression.
int
- Matches zero or any positive integer. Returns an int
.
slug
- Matches any slug
string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example,
building-your-1st-django-site.
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.
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_]+'
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?
否,在django-2.x, you can use path
[Django-doc] or re_path
[Django-doc]. Furthermore as of today, 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:
str
- Matches any non-empty string, excluding thepath
separator,'/'
. This is the default if a converter isn't included in the expression.int
- Matches zero or any positive integer. Returns anint
.slug
- Matches anyslug
string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.uuid
- Matches a formattedUUID
. 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 aUUID
instance.path
- Matches any non-empty string, including thepath
separator, '/'. This allows you to match against a complete URLpath
rather than just a segment of a URLpath
as withstr
.
我们可以在这里使用 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_]+'