S
S
Sergey Nizhny Novgorod2016-01-18 12:55:33
Django
Sergey Nizhny Novgorod, 2016-01-18 12:55:33

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)

Form output:
{% block question_area %}
  {% if allright %}
    <label class='error'>{{allright}}</label>
  {% endif %}
  ....
{% endblock %}

Problems:
1) Text does not appear. (if you specify a hard link. The hard link option is not immediately ok, since these links are formed dynamically).
2) It is not possible to make a redirect to itself, options with "", "#" - do not plow.
That is, how to display an additional field after processing the form, how to make a redirect to yourself in a view.
Update:
If we specify render and data, then an empty page is generated.
The page itself, where the question is located, is generated as follows:
1) The question_id is passed through the URL, which further helps the djang to understand which page needs to be rendered.
url(r'^question(?P<question_id>[0-9]+)$', 'bakot.views.question', name='question')

2) The view itself for the page:
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)

And here it turns out that the form is processed, but in the end there is no return to site.ru/question1 . Those. I can't figure out how the form will return the visitor to the same page.
Update2
I made this function:
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)

As a result, it turns out that we are on the site.ru/question1 page - we click, we are transferred to site.ru/answerset, where all the correct information is displayed. On this page, the question_id parameter (since it is not passed through the URL) is not set, and when you try to enter your data again, the page crashes.
Here, either you need to do something with these parameters, or you need to understand why we are being transferred not to site.ru/question1, but to site.ru/answerset

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question