All Projects → raiderrobert → Django Multiurl

raiderrobert / Django Multiurl

Licence: bsd-3-clause
Have you ever wanted multiple views to match to the same URL? Now you can.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Multiurl

Djurl
Simple yet helpful library for writing Django urls by an easy, short and intuitive way.
Stars: ✭ 85 (-68.28%)
Mutual labels:  django, routing, url
Ffrouter
Powerful and easy-to-use URL routing library in iOS that supports URL Rewrite(强大、易用、支持 URL Rewrite的 iOS 路由库)
Stars: ✭ 263 (-1.87%)
Mutual labels:  routing, url
Url Parser
Parse URLs into nicely structured data
Stars: ✭ 118 (-55.97%)
Mutual labels:  routing, url
React Easy Params
🔗 Auto synchronize your state with the URL and LocalStorage.
Stars: ✭ 73 (-72.76%)
Mutual labels:  routing, url
url
Build and parse URLs. Useful for HTTP and "routing" in single-page apps (SPAs)
Stars: ✭ 69 (-74.25%)
Mutual labels:  url, routing
node-match-path
Matches a URL against a path. Parameters, wildcards, RegExp.
Stars: ✭ 30 (-88.81%)
Mutual labels:  url, routing
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+251.12%)
Mutual labels:  routing, url
Django Macros Url
Django Macros URL. Routing must be simple as possible
Stars: ✭ 121 (-54.85%)
Mutual labels:  django, routing
url-trailing-slash
Allows enforcing URL routes with or without trailing slash
Stars: ✭ 35 (-86.94%)
Mutual labels:  url, routing
golgi
A composable routing library for Haxe.
Stars: ✭ 37 (-86.19%)
Mutual labels:  url, routing
Django Mptt Admin
Django-mptt-admin provides a nice Django Admin interface for Mptt models
Stars: ✭ 256 (-4.48%)
Mutual labels:  django
Django Elasticsearch Dsl Drf
Integrate Elasticsearch DSL with Django REST framework.
Stars: ✭ 258 (-3.73%)
Mutual labels:  django
Myblog
Python+Django+MySQL 博客系统
Stars: ✭ 263 (-1.87%)
Mutual labels:  django
Gitlit
Platform to connect contributors and projects based on skill level and shared interests.
Stars: ✭ 265 (-1.12%)
Mutual labels:  django
Django Front
Django-front is a front-end editing Django application
Stars: ✭ 257 (-4.1%)
Mutual labels:  django
Django Hashid Field
Django Model Field that uses Hashids to obscure the value
Stars: ✭ 256 (-4.48%)
Mutual labels:  django
Django Bulma
Bulma theme for Django
Stars: ✭ 257 (-4.1%)
Mutual labels:  django
Django Vue.js Blog
django-vue.js-blog
Stars: ✭ 256 (-4.48%)
Mutual labels:  django
Django Admin Easy
Collection of admin fields and decorators to help to create computed or custom fields more friendly and easy way
Stars: ✭ 265 (-1.12%)
Mutual labels:  django
Reactor
Phoenix LiveView but for Django
Stars: ✭ 258 (-3.73%)
Mutual labels:  django

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/>.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].