Dan
Dan

Reputation: 641

Polls application — django tutorial not working

I am working through the Polls tutorial for Django. I have made it as far as the start of part six.

For some reason all of my class based generic views are working EXCEPT the class based index view. When trying to load localhost:8000/ I get the following error:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/

The current URL, , didn't match any of these.

Here is my mysite/urls.py:

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
   url(r'^polls/', include('polls.urls')),
   url(r'^admin/', admin.site.urls),
]

And here is my polls/urls.py

from django.conf.urls import url

from . import views

app_name = 'polls'

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'),
]

And here is the polls/views.py. I am just pasting the IndexView portion. The rest of the class based views are currently working:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.utils import timezone

from .models import Choice, Question

# Create your views here.
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        # Return last five published questions (not inc. future)
        return Question.objects.filter(
        pub_date__lte=timezone.now()
        ).order_by('-pub_date')[:5]

Am I missing something? Any help would be greatly appreciated.

Upvotes: 3

Views: 3265

Answers (1)

Alasdair
Alasdair

Reputation: 308999

Your index url pattern is in polls/urls.py, which you included under r'^polls/' so you should access it at:

http://localhost:8000/polls/

Getting a 404 for http://localhost:8000/ is expected behaviour, because your main urls.py only includes urls at admin/ and polls/. You would have to add a url pattern with regex r'^$' to main/urls.py to stop the 404.

Upvotes: 5

Related Questions