Open rchiodo opened 2 months ago
it works with just this
from django.views import generic
class IndexView(generic.ListView):
template_name = "polls/index.html"
context_object_name = "latest_question_list"
def get_
so, probably some code before it is messing things up. can you share full code?
Here's the entire contents of that file at the point it fails:
from django.db.models import F, Q
from django.db.models.query import QuerySet
from django.shortcuts import get_object_or_404, render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.template import loader
from django.urls import reverse
from django.utils import timezone
from django.views import generic
from polls.models import Choice, Question
# Create your views here.
def index(request):
latest_question_list = Question.objects.order_by("-pub_date")[:5]
output = ", ".join([q.question_text for q in latest_question_list])
template = loader.get_template("polls/index.html")
context = { "latest_question_list": latest_question_list}
return HttpResponse(template.render(context, request))
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, "polls/detail.html", {"question": question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, "polls/results.html", {"question": question})
def vote(request, question_id):
question: 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/detail.html", {
"question": question,
"error_message": "You didn't select a choice"
})
else:
selected_choice.votes = F("votes") + 1
selected_choice.save()
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
class IndexView(generic.ListView):
template_name = "polls/index.html"
context_object_name = "latest_question_list"
def get_
Go through the Django tutorial.
When you get to this step here: https://docs.djangoproject.com/en/5.0/intro/tutorial04/#amend-views
And your index view looks like so:
Complete the
get_queryset
There will be an error on the
Any
. I expected it to auto import the Any.