Consider that I include namespaced reusable application:
考虑到我包括了名称空间可重用应用程序:
urlpatterns = patterns('',
# ella urls
url('^ella/', include('ella.core.urls', namespace="ella")),
)
Now, the Ella applications has urls like that:
现在,Ella应用程序有这样的url:
urlpatterns = patterns( '',
url( r'^(?P<category>[a-z0-9-/]+)/$', category_detail, name="category_detail" ),
# object detail
url( r'^(?P<category>[a-z0-9-/]+)/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<content_type>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
object_detail, name="object_detail" )
)
Now, calling {% url ella:category_detail category="cat" %}
works fine. However, when object tries to generate link to it's details, it calls
现在,调用{% url ella:category_detail类别=“cat”%}工作良好。但是,当对象试图生成链接到它的细节时,它调用。
from django.core.urlresolvers import reverse
url = reverse('object_detail', kwargs={'required' : 'params'})
This is not working, unless rewritten as
这不是工作,除非重写为。
from django.core.urlresolvers import reverse
url = reverse('ella:object_detail', kwargs={'required' : 'params'})
So, if I understand it correctly, including reusable application into namespace breaks all inner reverse()s inside given application.
因此,如果我正确地理解了它,将可重用的应用程序包括到名称空间中,就可以在给定的应用程序中打破所有内部反向()s。
Is it true? What have I missed? Is there any way around?
是真的吗?我错过了什么?有什么办法吗?
3 个解决方案
#1
6
Since you have name-spaced url configuration, you need to mention namespace:view-name pattern in order to reverse it properly (especially from view).
由于您有名称间隔的url配置,您需要提到名称空间:视图-名称模式,以便正确地反转它(尤其是从视图中)。
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
但是,如果您想避免这种情况,也可以将命名空间/appname作为current_app参数。在模板中有多种方法来指定current_app。但是如果您在视图中,您需要硬编码,或者传递到current_app参数。
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
参考:http://docs.djangoproject.com/en/dev/topics/http/urls/逆转
#2
0
URL Namespaces can be specified in two ways.
可以通过两种方式指定URL名称空间。
Firstly, you can provide the application and instance namespace as arguments to include() when you construct your URL patterns. For example,:
首先,当您构造URL模式时,您可以提供应用程序和实例名称空间作为参数来包含()。例如,:
(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),
(r ^ /帮助,包括(“apps.help。url”,名称空间=“foo”,app_name = '酒吧')),
This is right from http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces.
这是来自http://docs.djangoproject.com/en/dev/topics/http/urls/# define -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.
试着改变包括(“ella.core。url”,名称空间=“埃拉”)包括(ella.core。url”,名称空间=“埃拉”,app_name =“埃拉”)。我并不是100%的成功,但值得一试。
#3
0
At least in Django 1.8 you can write something like this:
至少在Django 1.8中可以这样写:
url = reverse('%s:object_detail' % request.resolver_match.namespace, kwargs={'required' : 'params'})
反向('%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.
resolver_match.namespace是一个字符串,包含当前运行视图的名称空间。
#1
6
Since you have name-spaced url configuration, you need to mention namespace:view-name pattern in order to reverse it properly (especially from view).
由于您有名称间隔的url配置,您需要提到名称空间:视图-名称模式,以便正确地反转它(尤其是从视图中)。
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
但是,如果您想避免这种情况,也可以将命名空间/appname作为current_app参数。在模板中有多种方法来指定current_app。但是如果您在视图中,您需要硬编码,或者传递到current_app参数。
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
参考:http://docs.djangoproject.com/en/dev/topics/http/urls/逆转
#2
0
URL Namespaces can be specified in two ways.
可以通过两种方式指定URL名称空间。
Firstly, you can provide the application and instance namespace as arguments to include() when you construct your URL patterns. For example,:
首先,当您构造URL模式时,您可以提供应用程序和实例名称空间作为参数来包含()。例如,:
(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),
(r ^ /帮助,包括(“apps.help。url”,名称空间=“foo”,app_name = '酒吧')),
This is right from http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces.
这是来自http://docs.djangoproject.com/en/dev/topics/http/urls/# define -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.
试着改变包括(“ella.core。url”,名称空间=“埃拉”)包括(ella.core。url”,名称空间=“埃拉”,app_name =“埃拉”)。我并不是100%的成功,但值得一试。
#3
0
At least in Django 1.8 you can write something like this:
至少在Django 1.8中可以这样写:
url = reverse('%s:object_detail' % request.resolver_match.namespace, kwargs={'required' : 'params'})
反向('%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.
resolver_match.namespace是一个字符串,包含当前运行视图的名称空间。