如何在 Django 命名空间可重用应用程序中进行反向 URL 搜索

发布于 2024-09-16 14:26:09 字数 1097 浏览 14 评论 0 原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2024-09-23 14:26:09

由于您具有名称空间 url 配置,因此需要提及名称空间:视图名称模式才能正确反转它(尤其是从视图中)。

但是,如果您想避免这种情况,您也可以将命名空间/应用程序名称作为 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

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

稍尽春風 2024-09-23 14:26:09

URL 命名空间可以通过两种方式指定。

首先,在构造 URL 模式时,您可以提供应用程序和实例命名空间作为 include() 的参数。例如:

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

这是来自 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% 这会起作用,但值得一试。

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.

烟酉 2024-09-23 14:26:09

至少在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文