Reversing namespaced URLs in Django: multiple instances of the same app Reversing namespaced URLs in Django: multiple instances of the same app django django

Reversing namespaced URLs in Django: multiple instances of the same app


A lot has changed since the question was posted but for future googlers (like myself) it might be useful to point out that request has the namespace now (at least since 1.7 as shown in this example.

From what I understood we should be able to simply pass current_app positional argument to reverse/redirect but I couldn't get it to work so I ended up creating an help method for this purpose:

def __redirect(request, viewname, *args, **kwargs):    to = viewname    if callable(to):        to = viewname.__module__ + '.' + viewname.__name__    if request.resolver_match.namespace:        to = '%s:%s' % (request.resolver_match.namespace, to)    return redirect(        to,        *args,        **kwargs    )

I'm using redirect here but all the arguments are passed on to reverse, so it's the same.


Not a very nice solution, but since you use the same text for your namespace and initial part of the URL path, you can extract that element from request.path (request.path.split('/')[1]) and set that as current_app in the request context, or just use it as the namespace in views.

http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces point 2.

You could do that e.g. in a context processor (if you want to use the namespace in a template).

For views you could write a decorator that feeds your function an extra kwarg "namespace" and use it as:

@feed_namespacedef view1(request, *args, **kwargs):    ns = kwargs['namespace']

or just write a reverse_namespaced function with an extra param (the request) where the function gets the namespace from, and use it instead of reverse.

Of course if you do this you will always have to use a request path/namespace for this app


There is a doc page about reversing namespaced urls.

http://docs.djangoproject.com/en/dev/topics/http/urls/#topics-http-reversing-url-namespaces

Either reverse('instance1:myapp.urls.some_view') or reverse('instance1:view_name') should work, or both :) - i've never tried this myself.