0
0 комментариев

Изучаю Джанго по документации, дошел до теста.
При

>>> from django.core.urlresolvers import reverse
>>> response = client.get(reverse('polls:index'))

выдает ошибку
KeyError: ‘polls’
NoReverseMatch: ‘polls’ is not a registered namespace
Вот мой urls.py

from django.conf.urls import include, url
from django.contrib import admin
 
urlpatterns = [
    url(r'^polls/', include('polls.urls', namespace = 'polls')),
    url(r'^admin/', admin.site.urls),
]

/polls/urls.py

from django.conf.urls import url
 
from . import views
 
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(),
name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

views.py(оставил только класс IndexView):

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
 
from .models import Choice, Question
 
 
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'
 
    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]

index.html

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}


Добавить комментарий