NotInternetExplorer
NotInternetExplorer

Reputation: 91

Django: Page not found (404)

So I'm going through the Django tutorial, using version 1.8

When I go to the page that should display the index, which is at

http://127.0.0.1:8000/

I get a 404 error which says

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

Using the URLconf defined in forumtest.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these.

These are my urls pages.

forumtest/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/', include(admin.site.urls)),
]

I'm also creating an app called polls with it's own urls.py

polls/urls.py :

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /polls/
    url(r'^$', views.IndexView.as_view(), name='index'),
    # ex: /polls/5/
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(),  name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

This is my views page :

polls/views.py :

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponseRedirect,HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic

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 the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

def vote(request,question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except(KeyError,Choice.DoesNotExist):
        #redisplay the question voting form
        return render(request,'polls/detail.html',{
            'question':p,
            'error_message': "you didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

I have no idea how to fix an issue like this, so if anyone can help that would be great.

Thanks.

Upvotes: 1

Views: 1771

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

The error message shows you what is wrong. You have defined urls under /polls/, so that is the address you should go to.

Upvotes: 2

Related Questions