django - Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ -
i following django documentation , making simple poll app. have come across following error :
using urlconf defined in mysite.urls, django tried these url patterns, in order: ^polls/ ^admin/ current url, , didn't match of these."
settings.py
root_urlconf = 'mysite.urls'
mysite/mysite/urls.py
from django.conf.urls import include,url django.contrib import admin urlpatterns = [ url(r'^polls/',include('polls.urls')), url(r'^admin/', admin.site.urls),]
mysite/polls/urls.py
from django.conf.urls import url . 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'),]
mysite/polls/views.py
from django.shortcuts import get_object_or_404,render django.http import httpresponseredirect, httpresponse django.core.urlresolvers import reverse django.views import generic django.utils import timezone django.template import loader .models import choice,question django.template.loader import get_template #def index(request): # return httpresponse("hello, world. you're @ polls index") class indexview(generic.listview): template_name='polls/index.html' context_object_name='latest_question_list' def get_queryset(self): """return last 5 published questions.""" return question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[5:] class detailview(generic.detailview): model=question template_name='polls/detail.html' def get_queryset(self): """ excludes questions aren't published yet. """ return question.objects.filter(pub_date__lte=timezone.now()) class resultsview(generic.detailview): model= question template_name ='polls/results.html' def vote(request, question_id): question=get_object_or_404(question, pk=question_id) try: selected_choice= question.choice_set.get(pk=request.post['choice']) except (keyerror, choice.doesnotexist): return render(request, 'polls/details.html', { 'question':question, 'error_message' : "you didn't select choice" , }) else: selected_choice.votes+=1 selected_choice.save() return httpresponseredirect(reverse('polls:results', args=(question.id,)))
index.html
<!doctype html > {% load staticfiles %} <html> <body> <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> {% if latest_question_list %} <ul> {% question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}">{{question.question_test }} </a></li> {% endfor %} </ul> {% else %} <p>no polls available.</p> {% endif %} </body> </html>
this link http://127.0.0.1:8000/polls/ shows blank page 3 bullets. (i have 3 questions in database , id's 5,6,7 because have been deleting , adding questions.)
my admin works fine!
i'm new django , have been searching , asking around , have been stuck on while now.
you 404 on http://127.0.0.1:8000/
because have not created url patterns url. have included url http://127.0.0.1:8000/polls/
, because have included polls urls with
url(r'^polls/',include('polls.urls')),
the empty bullets suggest there problem polls/index.html
template. looks have typo , have put {{ question.question_test }}
instead of {{ question.question_text }}
. make sure matches template tutorial 3:
{% if latest_question_list %} <ul> {% question in latest_question_list %} <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>no polls available.</p> {% endif %}
Comments
Post a Comment