开发者

Anyone knows good Django URL namespaces tutorial? [closed]

Closed. This question does not me开发者_Python百科et Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

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.


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

In apps.help.urls:

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

In your main 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 otherhelp:index %}

should produce the url /ineedhelp/.

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

should likewise produce the url /ineedhelp/.

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

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

reverse('help:index', current_app='otherhelp') should likewise produce /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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜