Answer the question
In order to leave comments, you need to log in
How to process to display new data in the template on the same page in Django?
Guys, hello.
Task: Questionnaire. The person selects the values, presses the "check result" button. If everything is OK, then on the same page an inscription appears that everything is fine, if there are errors, then an inscription appears that everything is bad.
View:
def answerset(request):
done = csrf(request)
if request.POST:
question1 = request.POST.get('question1', '')
question2 = request.POST.get('question2', '')
question3 = request.POST.get('question3', '')
if question1 == 'answertrue' and question2 == 'answertrue' and question3 == 'answertrue':
allright = {'allright' : "Все верно"}
return redirect('#', done, allright )
else:
allwrong = {'allwrong' : "Попробуйте еще раз"}
return redirect('', done, allwrong)
{% block question_area %}
{% if allright %}
<label class='error'>{{allright}}</label>
{% endif %}
....
{% endblock %}
url(r'^question(?P<question_id>[0-9]+)$', 'bakot.views.question', name='question')
def question(request, question_id):
questionship = Question.objects.filter(step_id = question_id)
answership = questionship.prefetch_related().all #https://docs.djangoproject.com/es/1.9/ref/models/relations/
context = {
"questionship" : questionship,
"answership" : answership,
}
return render(request, 'bakot/question.html', context)
def question(request, question_id):
questionship = Question.objects.filter(step_id = question_id)
answership = questionship.prefetch_related().all #https://docs.djangoproject.com/es/1.9/ref/models/relations/
question_id = question_id
context = {
"questionship" : questionship,
"answership" : answership,
"question_id" : question_id
}
return render(request, 'bakot/question.html', context)
def answerset(request):
done = csrf(request)
if request.POST:
page_number = str(request.POST.get('mono', '')) #Вставил на страницу параметр, который генерируется автоматически из question_id.
question1 = request.POST.get('question1', '')
question2 = request.POST.get('question2', '')
question3 = request.POST.get('question3', '')
if question1 == 'answertrue' and question2 == 'answertrue' and question3 == 'answertrue':
allright = "Пользователь не найден"
questionship = Question.objects.filter(step_id = page_number)
answership = questionship.prefetch_related().all
contex = {
"questionship" : questionship,
"answership" : answership,
"allright" : allright
}
done.update(contex)
return render(request, 'bakot/question.html', done)
else:
allwrong = {'allwrong' : "Пользователь не найден"}
return redirect('', done, allwrong)
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