如何在可重用应用程序中将名称空间 url 与 django 一起使用

发布于 2024-09-07 23:11:21 字数 643 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

寂寞陪衬 2024-09-14 23:11:21

据我所知,您应该能够按照您的描述使用 {% url forum:thread thread %} 。命名空间似乎总是由两个变量定义:namespace 和 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('forum.urls', namespace='bar', app_name='forum')),

根据我的理解,这定义了应用程序“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:

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.

江挽川 2024-09-14 23:11:21

根据我对这个问题的理解:

在 Django 2.1.7 中

  • 您可以在
# app's urls.py
from django.urls import path
from . import views

app_name = 'forum'
urlpatterns = [
    path('thread/', views.mark_done, name='thread')
]

主 urls.py

# urls.py
....

urlpatterns = [
    path('forum/', include('forum.urls')),
]

中应用程序的 urls.py 文件中的应用程序名称 ,然后您可以在您的应用程序中使用 {% url 'forum:thread' %}模板

  • 如果你想在 for 循环中使用它

我认为我们应该

  1. 创建一个视图,返回所有线程 作为上下文
  2. ,然后添加到该视图的路径,
...
path('thread/<int:pk>', views.mark_done, name='thread')

模板中的 url 如下:

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

based on my understanding of this question:

in Django 2.1.7

  • You can app name in app's urls.py file
# app's urls.py
from django.urls import path
from . import views

app_name = 'forum'
urlpatterns = [
    path('thread/', views.mark_done, name='thread')
]

in main urls.py

# urls.py
....

urlpatterns = [
    path('forum/', include('forum.urls')),
]

then you can employ {% url 'forum:thread' %} in your template

  • If you wanna use it in a for loop

I think we should

  1. create a view return all threads as context
  2. then add a path to that view
...
path('thread/<int:pk>', views.mark_done, name='thread')

the url in template will like:

{% for thread in threads %}
    <a href="{% url 'forum:thread' thread.id %}">{{thread.title}}</a>
{% endfor %}
夜清冷一曲。 2024-09-14 23:11:21

这可能是一个简单的语法错误。我正在遵循 Django 教程,并且我不正确地更改了 mysite/urls.py 。原始语法:

url(r'^polls/', include('polls.urls')),

所需的更改:

url(r'^polls/', include('polls.urls', namespace="polls")),

我做了什么:

url(r'^polls/', include('polls.urls'), namespace="polls"),

更正语法解决了问题。

This might be a simple syntax error. I was following the Django Tutorial, and I changed mysite/urls.py improperly. The original syntax:

url(r'^polls/', include('polls.urls')),

The desired change:

url(r'^polls/', include('polls.urls', namespace="polls")),

What I did:

url(r'^polls/', include('polls.urls'), namespace="polls"),

Correcting the syntax resolved the issue.

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