使 Regex Django URL 令牌成为可选

发布于 2024-08-23 11:39:12 字数 624 浏览 5 评论 0原文

您有一个在 Django 中接受 first_namelast_name 的 URL:

('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/$','some_method'),

如何在不创建任何新行的情况下包含 title 的可选 URL 标记。我的意思是,在理想的情况下:

#A regex constant
OP_REGEX = r'THIS IS OPTIONAL<title>[a-z]'
#Ideal URL
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/OP_REGEX/$','some_method'),

这是否可以在不创建新行的情况下实现,即

('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/(?P<title>[a-zA-Z]+)/$','some_method'),

You have a URL which accepts a first_name and last_name in Django:

('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/

How would you include the OPTIONAL URL token of title, without creating any new lines. What I mean by this is, in an ideal scenario:

#A regex constant
OP_REGEX = r'THIS IS OPTIONAL<title>[a-z]'
#Ideal URL
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/OP_REGEX/

Is this possible without creating a new line i.e.

('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)/(?P<title>[a-zA-Z]+)/
,'some_method'),

How would you include the OPTIONAL URL token of title, without creating any new lines. What I mean by this is, in an ideal scenario:


Is this possible without creating a new line i.e.


,'some_method'),

Is this possible without creating a new line i.e.


,'some_method'),

How would you include the OPTIONAL URL token of title, without creating any new lines. What I mean by this is, in an ideal scenario:

Is this possible without creating a new line i.e.

,'some_method'),,'some_method'),

How would you include the OPTIONAL URL token of title, without creating any new lines. What I mean by this is, in an ideal scenario:

Is this possible without creating a new line i.e.

,'some_method'),

Is this possible without creating a new line i.e.

,'some_method'),

How would you include the OPTIONAL URL token of title, without creating any new lines. What I mean by this is, in an ideal scenario:

Is this possible without creating a new line i.e.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

温柔女人霸气范 2024-08-30 11:39:12
('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)(?:/(?P<title>[a-zA-Z]+))?/

不要忘记在视图中为 title 指定默认值。

,'some_method'),

不要忘记在视图中为 title 指定默认值。

('^(?P<first_name>[a-zA-Z]+)/(?P<last_name>[a-zA-Z]+)(?:/(?P<title>[a-zA-Z]+))?/

Don't forget to give title a default value in the view.

,'some_method'),

Don't forget to give title a default value in the view.

帥小哥 2024-08-30 11:39:12

如果您正在寻找多个可选参数,而没有任何必需的参数,只需在开头省略“/”,例如:

re_path(r'^view(?:/(?P<dummy1>[a-zA-Z]+))?(?:/(?P<dummy2>[a-zA-Z]+))?(?:/(?P<dummy3>[a-zA-Z]+))?/

您可以浏览:

http://localhost:8000/view/?dummy1=value1&dummy2=value2&dummy3=value3
, views.MyView.as_view(), name='myname'),

您可以浏览:

In case your are looking for multiple optional arguments, without any required ones, just omit "/" at the beginning, such as:

re_path(r'^view(?:/(?P<dummy1>[a-zA-Z]+))?(?:/(?P<dummy2>[a-zA-Z]+))?(?:/(?P<dummy3>[a-zA-Z]+))?/

which you can browse at:

http://localhost:8000/view/?dummy1=value1&dummy2=value2&dummy3=value3
, views.MyView.as_view(), name='myname'),

which you can browse at:

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