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

前端 未结 5 1825
鱼传尺愫
鱼传尺愫 2020-12-30 07:09

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 p

5条回答
  •  遥遥无期
    2020-12-30 07:21

    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

提交回复
热议问题