Multiple values ​​of Django input fields with the same name

I need help. How can I process a form with multiple input field values ​​with the same name? and only one time view, this is actually for the main questions of the form .. another idea i found this method from https://stackoverflow.com/a/212960/ ... :

relations = request.POST.getlist('relations')

django question form

How do I handle all this? I am currently doing this with <input type="radio"..., but of course, it cannot work if it has the same name in the form. But if I use:, the <input type="checkbox"...answers can be checked for more than one question ...

Maybe so:

<input type="radio" name="answer-{{ question.id }}">

How can I get all this in a view?

It is decided:

In my test:

{% for question in questions %}
    <input type="hidden" name="question" value="{{ question.id }}/>

    {% for answer in question.get_answers %}
        <input type="radio" name="answer-{{ question.id }}" value={{ answer.score }}>
    {% endfor %}
{% endfor %}

views.py

questions = request.POST.getlist('question')
answers = [request.POST['answer-{}'.format(q)] for q in questions]

And its results:

['20', '19', '16', '13', '11', '10', '9', '8', '1']
['5', '2', '3', '4', '1', '4', '4', '2', '2']
+4
source share
1

, ? :

{% for answer in answers %}
    <input type="checkbox" name="answer" id="answer{{ forloop.counter }}" value="{{ answer.id }}">
{% endif %}

Andi in view:

answer = request.POST.getlist('answer')
for el in answer:
    pass
+1

Source: https://habr.com/ru/post/1661822/


All Articles