如何在可重用应用程序中将名称空间 url 与 django 一起使用
我有一个 django 应用程序,一个论坛应用程序,其中包含模板。在这些模板中,有一些 URL 指向应用程序的某些部分。例如,thread_list 模板具有指向每个线程的链接,如下所示:
{% for thread in threads %}
<a href="{% url forum_thread thread %}">{{thread.title}}</a>
{% endfor %}
问题是,我不太喜欢将我的网址称为“forum_thread”。我更喜欢“线程”并使用 django 的命名空间功能。 “forum_thread”可能会在项目中的其他地方使用(命名空间冲突)。所以它看起来像这样:
{% for thread in threads %}
<a href="{% url forum:thread thread %}">{{thread.title}}</a>
{% endfor %}
但这感觉不是正确的方法。这里的文档有点不清楚。
我希望这个应用程序可重复使用且易于配置。但我也想用最好的标准。我不想让用户指定他们自己的命名空间名称,然后让他们编辑每个模板中的每个 URL。
我应该如何在这个应用程序中处理网址?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,您应该能够按照您的描述使用 {% url forum:thread thread %} 。命名空间似乎总是由两个变量定义:namespace 和 app_name。
如果您随后在 urls.py 中执行以下操作:
根据我的理解,这定义了应用程序“forum”、“foo”、“bar”和默认值(具有命名空间==app_name)的 3 个实例。
当您反转 forum:thread 时,它会使用当前上下文来确定要使用哪一个 - 如果您位于名称空间“foo”中,它将使用该上下文,否则它将使用默认值。
如果有人能够澄清 Django 如何决定“当前”命名空间/应用程序是什么,那将非常有帮助。我目前将其归类为“黑魔法”。
对命名空间和 app_name 之间的实际差异进行一些澄清也会有所帮助 - 我可能完全颠倒了这一点。当前的文档非常模糊。
注意:我可以将其用于初始请求,但目前无法将其用于 AJAX 请求 - 由于某种原因,这些请求始终使用默认实例。
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:
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.
根据我对这个问题的理解:
主 urls.py
中应用程序的 urls.py 文件中的应用程序名称 ,然后您可以在您的应用程序中使用
{% url 'forum:thread' %}
模板我认为我们应该
线程
作为上下文模板中的 url 如下:
based on my understanding of this question:
in main urls.py
then you can employ
{% url 'forum:thread' %}
in your templateI think we should
threads
as contextthe url in template will like:
这可能是一个简单的语法错误。我正在遵循 Django 教程,并且我不正确地更改了 mysite/urls.py 。原始语法:
所需的更改:
我做了什么:
更正语法解决了问题。
This might be a simple syntax error. I was following the Django Tutorial, and I changed mysite/urls.py improperly. The original syntax:
The desired change:
What I did:
Correcting the syntax resolved the issue.