Skip to main content

Allow multiple views to match the same URL.

Project description

django-multiurl
===============


------------------------------------------------------------------------------------------------------------------------------

.. image:: https://travis-ci.org/raiderrobert/django-multiurl.svg?branch=master
:target: https://travis-ci.org/raiderrobert/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 using ``get_object_or_404``, or you're using third-party views
that you can't modify to raise ``ContinueResolving``, you can pass a
``catch`` argument into ``multiurl`` to control which exceptions are
considered "continue" statements. For example, to allow ``Http404``
exceptions 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, ``catch`` should be a tuple of exceptions. It's probably a
good idea to always include ``ContinueResolving`` in the tuple.

* If the last view in a ``multiurl`` raises ``ContinueResolving`` (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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-multiurl-1.1.0.zip (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_multiurl-1.1.0-py2.py3-none-any.whl (3.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-multiurl-1.1.0.zip.

File metadata

  • Download URL: django-multiurl-1.1.0.zip
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-multiurl-1.1.0.zip
Algorithm Hash digest
SHA256 8321080e70f6c87b513d344244d84bca903ee460e406c8c63ea05485b10aefb8
MD5 a28e0d56447cd91c2a717e63a17356e6
BLAKE2b-256 fca64a2c0ef895c4ebbb8ea0ca2fedc5b70bd1cc7298cd115d050e31a2a046b5

See more details on using hashes here.

File details

Details for the file django_multiurl-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_multiurl-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2c6770a0fae4e30dec6f2d7b9a068fe1ca25d24e7cefee618ce5fe71b1e456fa
MD5 c50311564482c76479200807fbaeaafd
BLAKE2b-256 4c71b394c73820175e9c166d4dfec49b936bc3c9b37efce524c96a4a56e80723

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page