如何在Django命名空间可重用应用程序中进行反向URL搜索 [英] How to do reverse URL search in Django namespaced reusable application

查看:69
本文介绍了如何在Django命名空间可重用应用程序中进行反向URL搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我包括命名空间可重用应用程序:

  urlpatterns = pattern('',
#ella urls
url('^ ella /',include('ella.core.urls',namespace = ella)),

现在,Ella应用程序具有以下网址:

  urlpatterns =模式('',
url(r'^(?P< category> [a-z0-9-/] +)/ $',category_detail,name = category_detail),
#对象详细信息
url(r'^(?P [a-z0-9-/] +)/(?P dd {4})/(?P d {1,2})/(?P< daydd {1,2})/(?P< content_type> [a-z0-9-] +)/(?P< slug> [a-z0 -9-] +)/ $',
object_detail,name = object_detail)

现在,调用 {%url ella:category_detail category = cat%} 可以正常工作。但是,当对象尝试生成指向其详细信息的链接时,它将调用



从django.core.urlresolvers导入反向
url = reverse('object_detail',kwargs = {'required':'params'})

此无法正常工作,除非从django.core.urlresolvers重写为$ p

  b 
url = reverse('ella: object_detail',kwargs = {'required':'params'})

所以,如果我理解的话正确地,将可重复使用的应用程序包含到命名空间中会破坏给定应用程序内部的所有内部reverse()



是真的吗?我错过了什么?有什么办法吗?

解决方案

由于您具有以名称分隔的url配置,因此需要提及namespace:view-name pattern



但是,如果要避免这种情况,还可以将名称空间/应用程序名称作为current_app参数传递。
在模板中时,有多种方法可以指定current_app。但是,如果您在视线范围内,则需要像以前一样进行硬编码,或将其传递给current_app参数

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

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


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-/]+)/$', 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

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?

解决方案

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

这篇关于如何在Django命名空间可重用应用程序中进行反向URL搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆