How to do reverse URL search in Django namespaced reusable application

旧时模样 提交于 2019-12-02 18:35:20

Since you have name-spaced url configuration, you need to mention namespace:view-name pattern in order to reverse it properly (especially from view).

But, if you want to avoid this, you may also pass namespace/appname as current_app parameter. There are multiple ways to specify current_app when you are in template. But if you are in view, you need to hard-code as you did, or pass to current_app parameter

url = reverse('object_detail', 
              kwargs={'foo':'bar'}, 
              current_app=app_name_or_name_space)

refer: http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse

URL Namespaces can be specified in two ways.

Firstly, you can provide the application and instance namespace as arguments to include() when you construct your URL patterns. For example,:

(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),

This is right from http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces.

Try changing include('ella.core.urls', namespace="ella") to include('ella.core.urls', namespace="ella", app_name="ella"). I'm not 100% this will work, but its worth a shot.

At least in Django 1.8 you can write something like this:

url = reverse('%s:object_detail' % request.resolver_match.namespace, kwargs={'required' : 'params'})

request.resolver_match.namespace is a string containing the namespace of the currently running view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!