Reputation: 811
How to make two URLs with same structure make work?
I don't want to add any prefix before the URL. I want all views to be example.com/[slug]
.
Here are my URLs:
url(r'^(?P<slug>[-_\w]+)', views.CategoryArticlesView.as_view(), name='single_category'),
url(r'^(?P<slug>[-_\w]+)', views.SingleArticleView.as_view(), name='single_article'),
I can easily make it work by adding a prefix before first URL like:
url(r'^**category/**(?P<slug>[-_\w]+)', views.CategoryArticlesView.as_view(), name='single_category'),
but I want it without prefix. Now it matches only first URL but not the second.
Upvotes: 2
Views: 118
Reputation: 19816
You can not do what you want that way. Instead, you may add another view where you can dispatch your requests based on some condition like this:
def some_view(request):
if some_condition:
return CategoryArticlesView.as_view()(self.request)
else:
return SingleArticleView.as_view()(self.request)
Now, your url config can be:
url(r'^(?P<slug>[-_\w]+)', views.some_view, name='some_name'),
Upvotes: 3
Reputation: 1232
You can't create two urls with the same pattern car Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL. see how-django-processes-a-request
Upvotes: 2