How to use namespace urls with django in a reusuable app

后端 未结 3 1066
孤街浪徒
孤街浪徒 2020-12-14 08:16

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 li

相关标签:
3条回答
  • 2020-12-14 08:21

    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.

    0 讨论(0)
  • 2020-12-14 08:36

    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.

    0 讨论(0)
  • 2020-12-14 08:39

    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 %}
    
    0 讨论(0)
提交回复
热议问题