Reversing namespaced URLs in Django: multiple inst

2020-02-25 08:37发布

I've been working with Django for a while now (currently on version 1.2), but just recently started working on an app that needs to support multiple instances. E.g., the project urls.py file will include it twice, under two different namespaces, like this:

urlpatterns = patterns('',
    (r'^instance1/', include('myapp.urls', namespace='instance1')),
    (r'^instance2/', include('myapp.urls', namespace='instance2')),
)

I was going along fine, until I realized I needed to figure out what to do about all the internal calls to reverse() (or the template calls to the {% url %} filter). For instance, let's say I'm doing something like the following in one of my views:

return HttpResponseRedirect(reverse('view_name'))

or something like this in one of my templates:

<a href="{% url view_name %}">link text</a>

...where view_name is the name of a URL pattern contained in myapp.urls. Since I'm using namespaces, this will raise an error: there is no view called view_name. Rather, I have to tell it either instance1:view_name or instance2:view_name. But doing this dynamically is stumping me.

I did some looking and it looks like the current_app argument, passed to either Context or RequestContext, was designed to help with this, but it's not clear at all how to dynamically pass the right application name to current_app. So what's the right way to tell Django which namespace to use?

EDIT: My use case is to use a single installation of the app multiple times. That is, it only exists on disk once, but gets included multiple times in the project's root urls.py (each time under a different namespace, as in my example above). With this in mind, is there any good way to keep track of which namespace a view/template is being called from, and make any use of reverse() or {% url %} stick within the same namespace? I know Django 1.3 will provide some extra features that could help with this (namely, the new and improved resolve()), but surely there's a good way to do this now...

5条回答
够拽才男人
2楼-- · 2020-02-25 08:42

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_namespace
def 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

查看更多
我命由我不由天
3楼-- · 2020-02-25 08:49

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.

查看更多
等我变得足够好
4楼-- · 2020-02-25 08:58

If you're not too tied to namespaces, you could instead write your urls.py to be like:

urlpatterns = patterns('',
    (r'^(P<instance>/)', 'controllers.route_me'),
)

This will make a call to the controllers.route_me function and pass it the request, plus the string 'instance', which you could handle this way:

# (in controllers.py)
def route_me(request, instance):
    # you now have complete control, and do what you need to do with the 'instance' and 'request' vars
    pass
查看更多
成全新的幸福
5楼-- · 2020-02-25 09:00

The current_app variable is something you have to set yourself to something you like.

Personally I'd recommend setting it to something like __name__.rsplit('.', 1)[0] so you get spam in spam/views.py.

But you can define it to be anything you like, as long as your app name is consistent with what you define in your urls file.

查看更多
爷、活的狠高调
6楼-- · 2020-02-25 09:02

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.

查看更多
登录 后发表回答