如何处理可重用Django应用中的命名空间网址 [英] How to Deal with Namespacing URLs in a Reusable Django App

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

问题描述

我一直在努力使用可重用的Django应用倒转网址。这是我的问题的基本布局。



可重复使用的应用程序:



urls.py 文件具有以下模式:

  url(r '^ object-list / $',ObjectListView.as_view(),name ='object-list')

在可重用的应用程序的 views.py 中的某个地方,有以下反向调用:

 code> reverse('object-list')






我的项目



在我想使用此应用程序的项目中,我有另一个冲突的视图名称,所以我命名空间应用程序的URL如下所示:

  url(r'^ app-stuff /',include('app.urls',namespace = 'app'))








这样做会导致应用程序内的所有反向调用都抛出一个 NoReverseMatch 异常什么时候要查找对象细节,因为URL现在具有名称 app:object-detail



我可以访问有问题的应用程序,所以我可以修改它对反向的调用,但我想做它以一种不涉及硬编码命名空间的方式。我已经阅读了 current_app 命名空间参数到包含函数,但我不太明白他们是否会帮助解决我遇到的问题。



任何建议,您都可以非常感谢。 >

解决方案

我在阅读 Django的URL解析文档更为紧密。



如果你有可重用的应用程序称为 myapp ,您可以将属性 app_name 添加到其URL配置中,以赋予其应用程序命名空间。示例:

 #myapp / urls.py 

app_name ='myapp'

urlpatterns = [
url(r'^ index / $',IndexView.as_view(),name ='index')
]
pre>

现在,即使应用程序的URL包含如下:

 #project / urls.py 

urlpatterns = [
url(r'^ myapp /',include('myapp.urls',namespace ='random-namespace'))
]

...仍然可以使用 ('myapp:index')



如果你想更详细地看到这个,我创建了一个小例子的GitHub回购



编辑: strong>



指定 urls.py app_name 属性>只适用于Django 1.9及更高版本。对于Django 1.8,您必须使用 include()函数中的 app_name 参数。


I have been struggling with reversing URLs with a reusable Django app. Here's the basic layout of my problem.

Reusable App:

In the urls.py file it has the following pattern:

url(r'^object-list/$', ObjectListView.as_view(), name='object-list')

Somewhere in the reusable app's views.py, there is the following reverse call:

reverse('object-list')


My Project:

In the project where I want to use this app, I have another conflicting view name, so I namespace the app's URLs like so:

url(r'^app-stuff/', include('app.urls', namespace='app'))


The Problem:

This has the consequence of causing all reverse calls within the app to throw a NoReverseMatch exception when it tries to lookup object-detail, because the URL now has the name app:object-detail.

I have access to the app in question, so I can modify the calls it makes to reverse, but I would like to do it in a way that doesn't involve hardcoding a namespace. I have read the documentation on the current_app and namespace arguments to the include function but I don't quite understand if they would help solve the issue I'm having.

Any suggestions you might have are greatly appreciated.

解决方案

I solved this after reading Django's docs on URL resolution more closely.

If you have a reusable app called myapp, you can add the property app_name to its URL configuration to give it an application namespace. Example:

# myapp/urls.py

app_name = 'myapp'

urlpatterns = [
    url(r'^index/$', IndexView.as_view(), name='index')
]

Now, even if the app's URLs are included like so:

# project/urls.py

urlpatterns = [
    url(r'^myapp/', include('myapp.urls', namespace='random-namespace')),
]

...the urls are still accessible using reverse('myapp:index').

If you would like to see this in more detail, I created a small GitHub repo with an example.

Edit:

Specifying the app_name property in urls.py only works on Django 1.9 and up. For Django 1.8, you have to use the app_name parameter in the include() function.

这篇关于如何处理可重用Django应用中的命名空间网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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