有人知道Django URL名称空间教程吗?

时间:2022-09-12 18:06:28

I'm looking for a good tutorial for URL namespaces in Django. I find official documentation a little too sparse - it lacks good examples. I found similar question here on stack, but the answers didn't help me to fully understand the subject either.

我正在寻找一个关于Django中URL名称空间的很好的教程。我发现官方文件有点太稀疏了——它缺少好的例子。我在堆栈上发现了类似的问题,但是这些答案也不能帮助我完全理解这个问题。

2 个解决方案

#1


33  

Agreed, the docs for this are rather confusing. Here's my reading of it (NB: all code is untested!):

同意,这方面的文档相当令人困惑。以下是我对它的解读(NB:所有代码都没有经过测试!)

In apps.help.urls:

在apps.help.urls:

urlpatterns = [
    url(r'^$', 'apps.help.views.index', name='index'),
    ]

In your main urls.py:

在你的主要urls . py:

urlpatterns = [
    url(r'^help/', include('apps.help.urls', namespace='help', app_name='help')),
    url(r'^ineedhelp/', include('apps.help.urls', namespace='otherhelp', app_name='help')),
    ]

In your template:

在你的模板:

{% url help:index %}

should produce the url /help/.

应该生成url /帮助/。

{% url otherhelp:index %}

should produce the url /ineedhelp/.

应该生成url /ineedhelp/。

{% with current_app as 'otherhelp' %}
    {% url help:index %}
{% endwith %}

should likewise produce the url /ineedhelp/.

也应该生成url /ineedhelp/。

Similarly, reverse('help:index') should produce /help/.

同样,反向(“帮助:索引”)应该生成/帮助/。

reverse('otherhelp:index') should produce /ineedhelp/.

反向(“otherhelp:指数”)生产/ ineedhelp /。

reverse('help:index', current_app='otherhelp') should likewise produce /ineedhelp/.

反向('help:index', current_app='otherhelp')也应该生成/ineedhelp/。

Like I said, this is based on my reading of the docs and my existing familiarity with how things tend to work in Django-land. I haven't taken the time to test this.

就像我说的,这是基于我对文档的阅读,以及我对Django-land的工作方式的熟悉程度。我还没有花时间来测试。

#2


0  

This is from the docs

这是来自文档

(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),

Maybe you should be more specific about what you are trying to do.

也许你应该更具体地说明你想做什么。

#1


33  

Agreed, the docs for this are rather confusing. Here's my reading of it (NB: all code is untested!):

同意,这方面的文档相当令人困惑。以下是我对它的解读(NB:所有代码都没有经过测试!)

In apps.help.urls:

在apps.help.urls:

urlpatterns = [
    url(r'^$', 'apps.help.views.index', name='index'),
    ]

In your main urls.py:

在你的主要urls . py:

urlpatterns = [
    url(r'^help/', include('apps.help.urls', namespace='help', app_name='help')),
    url(r'^ineedhelp/', include('apps.help.urls', namespace='otherhelp', app_name='help')),
    ]

In your template:

在你的模板:

{% url help:index %}

should produce the url /help/.

应该生成url /帮助/。

{% url otherhelp:index %}

should produce the url /ineedhelp/.

应该生成url /ineedhelp/。

{% with current_app as 'otherhelp' %}
    {% url help:index %}
{% endwith %}

should likewise produce the url /ineedhelp/.

也应该生成url /ineedhelp/。

Similarly, reverse('help:index') should produce /help/.

同样,反向(“帮助:索引”)应该生成/帮助/。

reverse('otherhelp:index') should produce /ineedhelp/.

反向(“otherhelp:指数”)生产/ ineedhelp /。

reverse('help:index', current_app='otherhelp') should likewise produce /ineedhelp/.

反向('help:index', current_app='otherhelp')也应该生成/ineedhelp/。

Like I said, this is based on my reading of the docs and my existing familiarity with how things tend to work in Django-land. I haven't taken the time to test this.

就像我说的,这是基于我对文档的阅读,以及我对Django-land的工作方式的熟悉程度。我还没有花时间来测试。

#2


0  

This is from the docs

这是来自文档

(r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')),

Maybe you should be more specific about what you are trying to do.

也许你应该更具体地说明你想做什么。