Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 458
darkhorse Avatar asked Sep 17 '25 00:09

darkhorse


1 Answers

The following should do:

urlpatterns = [
    url(r'^(?P<content_type_name>comics|articles|videos)$', views.content_type, name='content_type'),
]
like image 149
Simon Charette Avatar answered Sep 18 '25 20:09

Simon Charette