如何在可重用的应用程序中使用django的命名空间url [英] How to use namespace urls with django in a reusuable app

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

问题描述

我有一个django应用程序,一个论坛应用程序,它有模板。在这些模板中,有指向应用程序部分的URL。例如,thread_list模板具有如下所示的每个线程的链接:

  {%线程中的线程%} 
< ; a href ={%url forum_thread thread%}> {{thread.title}}< / a>
{%endfor%}

事情是,我不喜欢打电话给我urlsforum_thread。我更喜欢线程,并使用django的命名空间功能。 forum_thread可能在项目中的其他位置(命名空间冲突)中使用。它将如下所示:

  {%线程中的线程%} 
< a href ={%url论坛:线程线程%}> {{thread.title}}< / a>
{%endfor%}

但这不是正确的做法这个。这里的文件有点不清楚。



我希望这个应用程序可以重复使用,易于配置。但我也想使用最好的标准。我不想让用户指定自己的命名空间名称,然后让他们在每个模板中编辑每个单个的URL。



我该怎么做网址在这个应用程序中?

解决方案

从我可以收集的内容可以使用{%url forum:thread thread%}已经描述过。命名空间似乎被定义为两个变量,命名空间和app_name。



如果您在urls.py中执行以下操作:

  url(r'^ / forum /',include('forum.urls',namespace ='forum',app_name ='forum')),
url(r'^ / foo /',include('forum.urls',namespace ='foo',app_name ='forum')),
url(r'^ / bar /',include .urls',namespace ='bar',app_name ='forum')),

,这定义了3个应用程序论坛,foo,bar和默认(具有namespace == app_name)的实例。



论坛:线程,它使用当前上下文来确定要使用哪一个上下文 - 如果你在命名空间'foo'中使用它,否则它会回退到默认值。



如果任何人能够澄清Django如何决定当前命名空间/应用程序将是非常有帮助的。我现在将其分类为黑魔法。



对于名称空间和app_name之间的实际区别的一些澄清也将是有帮助的 - 我有可能完全颠倒。目前的文档是非常模糊的。



注意:我有这个工作的初始请求,但是我目前无法为AJAX请求做这个工作 - 这些请求总是使用由于某些原因,默认实例。


I have a django app, a forum app, that has templates with it. In those templates, there are urls that point to parts of the app. For instance the thread_list template has links to each thread like so:

{% for thread in threads %}
    <a href="{% url forum_thread thread %}">{{thread.title}}</a>
{% endfor %}

The thing is, I don't really like calling my urls "forum_thread". I prefer just "thread" and using the namespace feature of django. "forum_thread" may be used somewhere else in the project (namespace collision).So it will look like this:

{% for thread in threads %}
    <a href="{% url forum:thread thread %}">{{thread.title}}</a>
{% endfor %}

but this doesn't feel like the correct way to do this. The docs are kind of unclear here.

I want this app to be reusable and easy to configure. But I also want to use the best standards. I don't want to have the to make the user specify their own namespace name, and then have them edit every single url in each template.

How should I do urls in this app?

解决方案

From what I can gather you should be able use {% url forum:thread thread %} as you've described. Namespaces always seem to be defined with two variables, namespace and app_name.

If you then do the following in urls.py:

url(r'^/forum/', include('forum.urls', namespace='forum', app_name='forum')),
url(r'^/foo/', include('forum.urls', namespace='foo', app_name='forum')),
url(r'^/bar/', include('forum.urls', namespace='bar', app_name='forum')),

In my understanding, this defines 3 instances of the app 'forum', 'foo', 'bar', and the default (which has namespace==app_name).

When you reverse forum:thread, it uses the current context to determine which one to use- if you are in namespace 'foo' it will use that, otherwise it will fall back on the default.

If anyone is able to clarify how Django decides what the 'current' namespace/app is that would be very helpful. I currently categorise it as 'black magic'.

Some clarification on the actual difference between namespace and app_name would also be helpful- it's possible that I have this totally reversed. The current docs are highly ambiguous.

Note: I have this working for initial requests, but I'm currently unable to make this work for AJAX requests- those always use the default instance for some reason.

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

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