使 Regex Django URL 令牌成为可选
您有一个在 Django 中接受 first_name
和 last_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要忘记在视图中为
title
指定默认值。Don't forget to give
title
a default value in the view.如果您正在寻找多个可选参数,而没有任何必需的参数,只需在开头省略“/”,例如:
您可以浏览:
In case your are looking for multiple optional arguments, without any required ones, just omit "/" at the beginning, such as:
which you can browse at: