Answer the question
In order to leave comments, you need to log in
What is this {'error_message': "you don't select a choice."} object?
There is this html code:
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
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/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
'question': question
, which is among the views in the views.py file, as I understand it, this expression refers to a django model called question. That is, there are two objects in the application named question. And what will happen if we refer to the name question from the html file? Python will search the code among the models in models.py and find the model question or it will search among the views in views.py and find the variable 'question': question which still refers us to the model and what's the point?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question