简体   繁体   中英

How to set up a Django URL regex that accepts only 3 specific words?

I have my urls.py set up as follow right now:

urlpatterns = [
url(r'^(?P<content_type_name>[a-zA-z-_]+)$', views.content_type, name = 'content_type'),
]

This makes it so that this url will accept any word or string of words separated by - or _ or both. However, I want a regex that will accept only one of three words for the content_type_name parameter - 'comics', 'articles', 'videos'.

How do I set this up?

The following should do:

urlpatterns = [
    url(r'^(?P<content_type_name>comics|articles|videos)$', views.content_type, name='content_type'),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM