Django Error u“'polls” is not a registered namespace

后端 未结 13 2602
误落风尘
误落风尘 2021-01-31 07:41

Yesterday I was working on my first app using this tutorial. It\'s a Poll and Choice app. The first page displays the question and when you click on the question it\'s suppose t

相关标签:
13条回答
  • 2021-01-31 08:23

    I think you missed the namespace:

    urlpatterns = patterns('',
        url(r'^polls/', include('polls.urls', namespace="polls")),
    )
    
    0 讨论(0)
  • 2021-01-31 08:25

    Django 2.0

    in yourapp/urls.py

    from django.urls import path
    from . import views
    
    app_name = 'yourapp'
    
    urlpatterns = [
        path('homepage/', views.HomepageView.as_view(), name='homepage'),
    ]
    

    in urls.py

    from django.contrib import admin
    from django.urls import path, include
    
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('yourapp/', include('yourapp.urls')),
        ]
    
    0 讨论(0)
  • 2021-01-31 08:25

    namespace should be added polls/urls.py file.

    url(r'^myapp/$', include('myapp.urls',  namespace ='myapp')),
    
    0 讨论(0)
  • 2021-01-31 08:26
     from django.conf.urls import patterns, include, url
     from django.contrib import admin
     from django.conf import settings
    
    
    
     urlpatterns = patterns('myapp.views',
         url(r'^$', 'index', name="index"),
         url(r'^(?P<poll_id>\d+)/$', 'detail', name="detail"),
         url(r'^(?P<poll_id>\d+)/results/$', 'results', name="results"),
         url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name="vote"),
    )
    
    ----------------------------------    
    
     <h1>{{ poll.question }}</h1>
    
     {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    
     <form method="post" action="{% url myapp:vote poll.id %}">
     {% csrf_token %}
     {% for choice in poll.choice_set.all %}
         <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
         <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
     {% endfor %}
     <input type="submit" value="Vote" />
     </form>
    
    0 讨论(0)
  • 2021-01-31 08:26

    I also faced the same issue. it is fixed now by adding app_name = "<name of your app>" in app/urls.py

    0 讨论(0)
  • 2021-01-31 08:26

    For anyone using "django-hosts":

    I had the same error and for me adding this to my template solved it (without any us of namespace etc.):

    {% load hosts %}
    <a href="{% host_url 'YOUR_URL' host 'YOUR_HOST' %}">Admin dashboard</a>
    

    Additionaly I added PARENT_HOST = 'YOUR_PARENT_HOST' to my settings.py

    Reference

    0 讨论(0)
提交回复
热议问题