django-multiurl


Namedjango-multiurl JSON
Version 1.5.0 PyPI version JSON
download
home_pagehttps://github.com/raiderrobert/django-multiurl
SummaryAllow multiple views to match the same URL.
upload_time2021-08-10 20:09:11
maintainer
docs_urlNone
authorJacob Kaplan-Moss and Robert Roskam
requires_python
licenseBSD
keywords django urls
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            django-multiurl
===============
.. image:: https://travis-ci.org/raiderrobert/django-multiurl.svg?branch=master
    :target: http://travis-ci.org/raiderrobert/django-multiurl
.. image:: https://coveralls.io/repos/github/raiderrobert/django-multiurl/badge.svg?branch=master
    :target: https://coveralls.io/github/raiderrobert/django-multiurl?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 <http://github.com/jacobian/django-multiurl>`_; pull requests are
welcome. Run tests with `tox <http://tox.readthedocs.org/>`_.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/raiderrobert/django-multiurl",
    "name": "django-multiurl",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "django urls",
    "author": "Jacob Kaplan-Moss and Robert Roskam",
    "author_email": "raiderrobert@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/09/ac/ca31cdca007879613099c8c289265b94d044ba4f282257ac241e2e4cac9d/django-multiurl-1.5.0.tar.gz",
    "platform": "",
    "description": "django-multiurl\n===============\n.. image:: https://travis-ci.org/raiderrobert/django-multiurl.svg?branch=master\n    :target: http://travis-ci.org/raiderrobert/django-multiurl\n.. image:: https://coveralls.io/repos/github/raiderrobert/django-multiurl/badge.svg?branch=master\n    :target: https://coveralls.io/github/raiderrobert/django-multiurl?branch=master\n    \nHave you ever wanted multiple views to match to the same URL? Now you can.\n\nYou may once have tried something like this::\n\n    urlpatterns = [\n        url('/app/(\\w+)/$', app.views.people),\n        url('/app/(\\w+)/$', app.views.place),\n    ]\n\nHowever, if you try this, ``/app/san-francisco/`` will only map to\n``app.views.people``. Raising an ``Http404`` from ``app.views.people`` doesn't\nhelp: you only get a 404 in the browser because Django stops resolving\nURLs at the first match.\n\nWell, ``django-multiurl`` solves this problem. Just \n``pip install django-multiurl``, then do this::\n\n    from multiurl import multiurl\n\n    urlpatterns = [\n        multiurl(\n            url('/app/(\\w+)/$', app.views.people),\n            url('/app/(\\w+)/$', app.views.place),\n        )\n    ]\n\nNow in your views, raise ``multiurl.ContinueResolving`` anywhere you'd like\nto break out of the view and keep resolving. For example, here's what\n``app.views.people`` might look like::\n\n    from multiurl import ContinueResolving\n\n    def people(request, name):\n        try:\n            person = Person.objects.get(name=name)\n        except Person.DoesNotExist:\n            raise ContinueResolving\n        return render(...)\n\nThat's it! ``ContinueResolving`` will cause ``multiurl`` to continue onto the\nnext view (``app.views.place``, in this example).\n\nA few notes to round things out:\n\n* If you don't want to use ``ContinueResolving`` -- perhaps you'd rather\n  continue using ``get_object_or_404``, or you're using third-party views\n  that you can't modify to raise ``ContinueResolving``, you can pass a\n  ``catch`` argument into ``multiurl`` to control which exceptions are\n  considered \"continue\" statements. For example, to allow ``Http404``\n  exceptions to continue resolving, do this::\n\n        urlpatterns = [\n            multiurl(\n                url('/app/(\\w+)/$', app.views.people),\n                url('/app/(\\w+)/$', app.views.place),\n                catch = (Http404, ContinueResolving)\n            )\n        ]\n\n  As you can see, ``catch`` should be a tuple of exceptions. It's probably a\n  good idea to always include ``ContinueResolving`` in the tuple.\n\n* If the last view in a ``multiurl`` raises ``ContinueResolving`` (or another\n  \"continuing\" exception), a 404 will be raised instead. That is, if resolving\n  \"falls off the end\" of some multi-urls, you'll get the 404 you expect.\n\n* Reverse URL resolution just works as expected. Name your sub-URLs and then\n  reverse your heart out.\n\nContributing\n------------\n\nDevelopment takes place\n`on GitHub <http://github.com/jacobian/django-multiurl>`_; pull requests are\nwelcome. Run tests with `tox <http://tox.readthedocs.org/>`_.\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Allow multiple views to match the same URL.",
    "version": "1.5.0",
    "project_urls": {
        "Download": "https://github.com/raiderrobert/django-multiurl/tarball/v1.5.0",
        "Homepage": "https://github.com/raiderrobert/django-multiurl"
    },
    "split_keywords": [
        "django",
        "urls"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9737de7ab76a7ede088b4d5c934c107a4e7818db45852045a37e3fd91f43f820",
                "md5": "8fa0a2940cc46b0544546ab8143c2798",
                "sha256": "66e804e311531c580654ec1b343074abaf6d08fb7520ed5be7b6b9a887a79259"
            },
            "downloads": -1,
            "filename": "django_multiurl-1.5.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8fa0a2940cc46b0544546ab8143c2798",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 4656,
            "upload_time": "2021-08-10T20:09:10",
            "upload_time_iso_8601": "2021-08-10T20:09:10.018948Z",
            "url": "https://files.pythonhosted.org/packages/97/37/de7ab76a7ede088b4d5c934c107a4e7818db45852045a37e3fd91f43f820/django_multiurl-1.5.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "09acca31cdca007879613099c8c289265b94d044ba4f282257ac241e2e4cac9d",
                "md5": "5efc70eebae59913da4fd51a258ada45",
                "sha256": "fddb8e43b8aef2172a46b6dc5522ea9f52ab2861049d00f4464448aead63f6b9"
            },
            "downloads": -1,
            "filename": "django-multiurl-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5efc70eebae59913da4fd51a258ada45",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4588,
            "upload_time": "2021-08-10T20:09:11",
            "upload_time_iso_8601": "2021-08-10T20:09:11.594785Z",
            "url": "https://files.pythonhosted.org/packages/09/ac/ca31cdca007879613099c8c289265b94d044ba4f282257ac241e2e4cac9d/django-multiurl-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-08-10 20:09:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "raiderrobert",
    "github_project": "django-multiurl",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "django-multiurl"
}
        
Elapsed time: 0.10304s