N
N
neulwert2019-12-17 17:02:35
Django
neulwert, 2019-12-17 17:02:35

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 %}

And there is one misunderstanding with the variable itself. I found the error message object only in this python code, after except:
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,)))

Please explain two variables: question and error message. They are in quotes and are in curly brackets. Looks like dictionaries, but I'm not sure. Explain, please, what is their meaning, why not create an ordinary variable and just refer to it?
And especially this code: '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

1 answer(s)
A
antonksa, 2019-12-17
@neulwert

@muxui
This is f...dets.
What the heck is writing if you don't understand what it is???!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question