I’ve got a URLs pattern like this:
urlpatterns = (
url(r'^$', list_titles, name='list'),
url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'),
url(r'^(?P<title>\S+?)/$', show_title, name='title'),
)
The filtered-list and title match the same things.
If there is an available list of things matching the tag in filtered-list, I want list_titles to fire off. But if there isn’t a matching tag, I want to bubble that back to the URL processor so show_title fires off.
If there’s no matching title, I’ll raise a proper 404 there.
I know I can do this from inside the view…but it’s a bit smelly having to hard-wire the process into the view. I’d like the URL order to decide what gets chosen first and what it hands off to.
This is certainly view logic; all urls.py is for is for matching URL patterns, not performing validation. You can use the
Http404exception to handle this.Alternatively, you may find the
get_object_or_404orget_list_or_404methods, which shorten it up a bit.Promised edit follows. Not exactly what you’re looking for, but…