Allow multiple views to match the same URL.
Project description
django-multiurl
Have you ever wanted multiple views to match to the same URL? Now you can.
You may once have tried something like this::
urlpatterns = [
url('/app/(\w+)/$', app.views.people),
url('/app/(\w+)/$', app.views.place),
]
However, if you try this, /app/san-francisco/ will only map to
app.views.people. Raising an Http404 from app.views.people doesn't
help: you only get a 404 in the browser because Django stops resolving
URLs at the first match.
Well, django-multiurl solves this problem. Just
pip install django-multiurl, then do this::
from multiurl import multiurl
urlpatterns = [
multiurl(
url('/app/(\w+)/$', app.views.people),
url('/app/(\w+)/$', app.views.place),
)
]
Now in your views, raise multiurl.ContinueResolving anywhere you'd like
to break out of the view and keep resolving. For example, here's what
app.views.people might look like::
from multiurl import ContinueResolving
def people(request, name):
try:
person = Person.objects.get(name=name)
except Person.DoesNotExist:
raise ContinueResolving
return render(...)
That's it! ContinueResolving will cause multiurl to continue onto the
next view (app.views.place, in this example).
A few notes to round things out:
-
If you don't want to use
ContinueResolving-- perhaps you'd rather continue usingget_object_or_404, or you're using third-party views that you can't modify to raiseContinueResolving, you can pass acatchargument intomultiurlto control which exceptions are considered "continue" statements. For example, to allowHttp404exceptions to continue resolving, do this::urlpatterns = [ multiurl( url('/app/(\w+)/$', app.views.people), url('/app/(\w+)/$', app.views.place), catch = (Http404, ContinueResolving) ) ]As you can see,
catchshould be a tuple of exceptions. It's probably a good idea to always includeContinueResolvingin the tuple. -
If the last view in a
multiurlraisesContinueResolving(or another "continuing" exception), a 404 will be raised instead. That is, if resolving "falls off the end" of some multi-urls, you'll get the 404 you expect. -
Reverse URL resolution just works as expected. Name your sub-URLs and then reverse your heart out.
Contributing
Development takes place
on GitHub <http://github.com/jacobian/django-multiurl>; pull requests are
welcome. Run tests with tox <http://tox.readthedocs.org/>.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_multiurl-1.5.1a0.tar.gz.
File metadata
- Download URL: django_multiurl-1.5.1a0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c6f40de44bcd8ba328a0d7996bec630387e8e74532d034777634bdc57615163
|
|
| MD5 |
0f919c514c7d8657844e904ab6c6f5f2
|
|
| BLAKE2b-256 |
c8e287df17d3bfece9f9aecb4a2ae6bbb0c091c08368c5562ee3113002f0a572
|
File details
Details for the file django_multiurl-1.5.1a0-py2.py3-none-any.whl.
File metadata
- Download URL: django_multiurl-1.5.1a0-py2.py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5ae6c47ad0d49c29f95df21d176619276028fe06e43f5b73f68553f00d97e96
|
|
| MD5 |
9a45ad1573ff114a8eb5fb528853bb04
|
|
| BLAKE2b-256 |
b7dadbded9cbb0fee6d52573ffb76d51da9d1b681090973ebbe36f0ef3c89d26
|