N
N
nanny_ogg2018-08-07 23:40:56
Python
nanny_ogg, 2018-08-07 23:40:56

How to get a list of arguments from a form in flask?

The problem is the following. There is a basket template, where ordered goods are transferred using the session. In the loop, the added products are displayed and the name of the product is written to the hidden input.

<form action="{{ url_for('send_to_manager') }}" method="POST">
    {% for product in session['cart'] %}
        <div>
            <h2>{{ product[0] }}</h2> 
            <div>{{ product[1] }}</div>
            <div>{{ product[2] }}</div>
        </div>
        <input type="hidden" name="product_name" value="{{ product[0] }}">
    {% endfor %}
    <button class="btn btn-warning btn-sm">Отправить заказ</button>
</form>

When submitting the form, if more than 1 product is added, it can be seen that a list is obtained. Using request.get_data(), you can get these parameters, but what to do next with them is not clear.
@app.route('/send_to_manager', methods=['POST', 'GET'])
def send_to_manager():
    data =  request.get_data()<code lang="python">
    return data
</code>

Таким образом не получается, возвращает только первый вариант
<code lang="python">
request.form['product_name']
</code>

В конечном итоге мне нужно получить список всех названий, которые пришли из поля формы
<code lang="python">
<input type="hidden" name="product_name" value="{{ product[0] }}">
</code>
и дальше с помощью списка или как-то еще записать в строку через запятые. Как можно это сделать?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
clojurerabbit, 2018-08-09
@nanny_ogg

request.form.getlist('product_name')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question