如何设置只接受 3 个特定单词的 Django URL 正则表达式?
How to set up a Django URL regex that accepts only 3 specific words?
我的 urls.py 现在设置如下:
urlpatterns = [
url(r'^(?P<content_type_name>[a-zA-z-_]+)$', views.content_type, name = 'content_type'),
]
这使得 url 将接受由 - 或 _ 或两者分隔的任何单词或单词字符串。但是,我想要一个正则表达式,它只接受 content_type_name 参数的三个单词之一 - 'comics'、'articles'、'videos'.
我该如何设置?
应执行以下操作:
urlpatterns = [
url(r'^(?P<content_type_name>comics|articles|videos)$', views.content_type, name='content_type'),
]
我的 urls.py 现在设置如下:
urlpatterns = [
url(r'^(?P<content_type_name>[a-zA-z-_]+)$', views.content_type, name = 'content_type'),
]
这使得 url 将接受由 - 或 _ 或两者分隔的任何单词或单词字符串。但是,我想要一个正则表达式,它只接受 content_type_name 参数的三个单词之一 - 'comics'、'articles'、'videos'.
我该如何设置?
应执行以下操作:
urlpatterns = [
url(r'^(?P<content_type_name>comics|articles|videos)$', views.content_type, name='content_type'),
]