在可重复使用的应用程序中作为嵌套应用程序使用的反向URL [英] Reverse url in reusable app that is consumed as a nested app

查看:67
本文介绍了在可重复使用的应用程序中作为嵌套应用程序使用的反向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处

我的处境略有不同。我创建了一个可重复使用的应用程序,称为类别。在我的项目中,我有一个名为 dashboard的应用程序。仪表板应用程序包括可重用的类别应用程序。这将导致以下内容用于反向url

My situation is slightly different though. I have created a reusable app called "categories". In my project I have an app called "dashboard". The dashboard app includes the reusable "categories" app. This causes the following to be used to reverse a url

reverse('dashboard:categories:browse')

但是,我的可重用应用程序不了解 dashboard命名空间。我希望能够使用上面链接的解决方案在可重复使用的类别应用中仅反转以下内容。

However, my reusable app has no knowledge of the "dashboard" namespace. I want to be able to use the solution I linked above to reverse only the following within the reusable categories app.

reverse('categories:browse')

当前,设置 app_name in Categories.urls不起作用。反转类别:浏览时,我得到 NoReverseMatch

Currently, setting app_name in categories.urls does not work. I get NoReverseMatch when reversing "categories:browse".

以下是这些应用程序如何包含在摘录中的摘录urls.py文件。

Here are excerpts of how the apps are included in the urls.py files.

# myproject/urls.py
url(
    r'^dashboard/',
    include(
        'dashboard.urls',
        namespace='dashboard',
    )
),


# dashboard/urls.py
url(
    r'^categories/',
    include(
        'categories.urls',
        namespace="categories",
    ),
),


推荐答案

您可以直接在主 urls.py 中包含类别网址:

You can include the categories urls in your main urls.py directly:

# myproject/urls.py
url(r'^dashboard/categories/', include('categories.urls', namespace='categories')),
url(r'^dashboard/', include('dashboard.urls', namespace='dashboard')),

这样,您的类别网址就不在嵌套的命名空间中,并且您只需使用 reverse(’categories:browse')

That way your categories urls are not in a nested namespace, and you can simply use reverse('categories:browse').

这篇关于在可重复使用的应用程序中作为嵌套应用程序使用的反向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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