【发布时间】:2017-02-08 11:23:48
【问题描述】:
目前在我的 urls.py 中有类似的内容:
url(r'^(?P<my_var>:|var1|var2|var3|other_string)/rest_of_url'
匹配/var1/rest_of_url
var2/rest_of_url
var3/rest_of_url
other_string/rest_of_url
我想要一个变量列表/元组:
VARIABLES = ('var1', 'var2', 'var3')
这可以存储可以在我的网址中匹配的变量。
但是我不知道任何可以做到这一点的 django 正则表达式。准确地说,我的目标是:
url(r'^(?P<my_var>:regex_matching_any_of_VARIABLES|other_string)/rest_of_url'
【问题讨论】:
-
您最好在视图中验证 my_var,如果 my_var 不在您接受的变量列表中,则返回 404。