Skip to main content
https://travis-ci.org/raiderrobert/django-multiurl.svg?branch=master https://coveralls.io/repos/github/raiderrobert/django-multiurl/badge.svg?branch=master

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; pull requests are welcome. Run tests with tox.

Release files for django-multiurl 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-multiurl 1.5.0
File Size Uploaded
django-multiurl-1.5.0.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-multiurl 1.5.0
File Interpreter ABI Platform
django_multiurl-1.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 9.2 kB

Release files / django-multiurl-1.5.0.tar.gz

Download URL django-multiurl-1.5.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fddb8e43b8aef2172a46b6dc5522ea9f52ab2861049d00f4464448aead63f6b9
BLAKE2b-256 checksum
How to use checksums
09acca31cdca007879613099c8c289265b94d044ba4f282257ac241e2e4cac9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

Release files / django_multiurl-1.5.0-py2.py3-none-any.whl

Download URL django_multiurl-1.5.0-py2.py3-none-any.whl
Size 4.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
66e804e311531c580654ec1b343074abaf6d08fb7520ed5be7b6b9a887a79259
BLAKE2b-256 checksum
How to use checksums
9737de7ab76a7ede088b4d5c934c107a4e7818db45852045a37e3fd91f43f820
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.4.0

1 release file

1.3.0

2 release files

1.2.0

1 release file

1.1.0

2 release files

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page