考虑到我包含命名空间可重用应用程序:
urlpatterns = patterns('',
# ella urls
url('^ella/', include('ella.core.urls', namespace="ella")),
)
现在,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" )
)
现在,调用 {% url ella:category_detail Category="cat" %}
工作正常。但是,当对象尝试生成指向其详细信息的链接时,它会调用
from django.core.urlresolvers import reverse
url = reverse('object_detail', kwargs={'required' : 'params'})
This 不起作用,除非重写为
from django.core.urlresolvers import reverse
url = reverse('ella:object_detail', kwargs={'required' : 'params'})
So,如果我理解正确的话,将可重用应用程序包含到命名空间中会破坏给定应用程序内的所有内部反向()。
这是真的吗?我错过了什么?有什么办法吗?
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:
urlpatterns = patterns( '',
url( r'^(?P<category>[a-z0-9-/]+)/
Now, calling {% url ella:category_detail category="cat" %}
works fine. However, when object tries to generate link to it's details, it calls
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.
Is it true? What have I missed? Is there any way around?
, 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-]+)/
Now, calling {% url ella:category_detail category="cat" %}
works fine. However, when object tries to generate link to it's details, it calls
This is not working, unless rewritten as
So, if I understand it correctly, including reusable application into namespace breaks all inner reverse()s inside given application.
Is it true? What have I missed? Is there any way around?
,
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
This is not working, unless rewritten as
So, if I understand it correctly, including reusable application into namespace breaks all inner reverse()s inside given application.
Is it true? What have I missed? Is there any way around?
发布评论
评论(3)
由于您具有名称空间 url 配置,因此需要提及名称空间:视图名称模式才能正确反转它(尤其是从视图中)。
但是,如果您想避免这种情况,您也可以将命名空间/应用程序名称作为 current_app 参数传递。
当您在模板中时,有多种方法可以指定 current_app 。但是,如果您在视图中,则需要像以前一样进行硬编码,或者传递给 current_app 参数
参考: http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse
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
refer: http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse
这是来自 http://docs.djangoproject.com/en/dev/topics/http/urls/#defining -url-命名空间。
尝试将
include('ella.core.urls', namespace="ella")
更改为include('ella.core.urls', namespace="ella", app_name="ella" )
。我不是 100% 这会起作用,但值得一试。This is right from http://docs.djangoproject.com/en/dev/topics/http/urls/#defining-url-namespaces.
Try changing
include('ella.core.urls', namespace="ella")
toinclude('ella.core.urls', namespace="ella", app_name="ella")
. I'm not 100% this will work, but its worth a shot.至少在 Django 1.8 中你可以这样写:
url = reverse('%s:object_detail' % request.resolver_match.namespace, kwargs={'required' : 'params'})
request.resolver_match.namespace
是一个包含当前运行视图的命名空间的字符串。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.