You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
969 B
33 lines
969 B
from django.http import Http404 |
|
|
|
|
|
def dispatch_slug_path(*views): |
|
""" |
|
Dispatch full path slug in iterating through a set of views. |
|
Http404 exceptions raised by a view lead to trying the next view |
|
in the list. |
|
|
|
This allows to plug different slug systems to the same root URL. |
|
|
|
Usages:: |
|
|
|
# in urls.py |
|
path('<slug:slug_path>/', dispatch_slug_path( |
|
views.CategoryDetailView.as_view(), |
|
views.ArticleDetailView.as_view())), |
|
) |
|
""" |
|
def wrapper(request, **kwargs): |
|
view_args = [] |
|
view_kwargs = {'url_path': kwargs[list(kwargs.keys())[0]]} |
|
|
|
not_found_exception = Http404 |
|
for view in views: |
|
try: |
|
return view(request, *view_args, **view_kwargs) |
|
except Http404 as e: |
|
not_found_exception = e # assign to use it outside of except block |
|
continue |
|
raise not_found_exception |
|
|
|
return wrapper
|
|
|