0

I have the following HTML form:

<form method="POST" action="/dashboard/account/assignkpibulk">
    <div class="modal-body form-group">
        Select from the <b>dropdown</b> the KPI to be assign to each campaign,
        then click <b>"Assign KPI´s"</b> to complete the assigment. If you want to assign a KPI to only one
        campaign click on <b>Assign KPI</b>
    </div>
    <div class="container">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table" id="modalKpiTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Campaign Name</th>
                            <th>Campaign ID</th>
                            <th>KPI Name</th>
                            <th class="text-center">Assigment Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for info in campaign_data %}
                        <tr>
                            <input type="hidden" name="campaign_name" value="{{info.campaign}}">
                            <td>{{info.campaign}}</td>
                            <input type="hidden" name="campaign_id" value="{{info.campaign_id}}">
                            <td>{{info.campaign_id}}</td>
                            <td class="text-center">
                                <div class="form-group">
    
                                    <select class="form-control form-control-sm" id="kpiFormControlSelect" name="kpi_name">
                                        <option value="none">None</option>
                                        {% for kpi in kpi_info %}
                                        <option value="{{kpi.kpi_name}}">{{ kpi.kpi_name }}</option>
                                        {% endfor %}
                                    </select>
    
                                </div>
                            </td>
                            <td class="text-center">
                                <a href="" class="d-none d-sm-inline-block btn btn-sm btn-info shadow-sm">
                                    <i class="fas fa-square-root-alt fa-sm text-white-50"></i> Assign KPI
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <br>
    <div class="modal-footer">
        <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
        <input class="btn btn-primary" type="submit" value="Assign KPI´s" href="">
    </div>
</form>

When I run the following function:

@blueprint.route('/account/assignkpibulk', methods=['GET','POST'])
@isLoggedIn
def assign_kpi_bluk():

    if request.method == 'POST':

        print(request.form.to_dict())

        _data = {'user_name': session['user_name'], 
                'id': None,
                'data': request.form.to_dict()}

    return redirect(url_for('dashboard.assign_kpi', client_id=session['client_id'], client_name=session['client_name']))

when I print(request.form.to_dict()) it only request the first value of the dictionary:

{'modalKpiTable_length': '10', 'campaign_name': 'Sample Campaign', 'campaign_id': '900990999', 'kpi_name': 'none'}

However if I do just print(request.form) I got all the results:

ImmutableMultiDict([('modalKpiTable_length', '3'), ('campaign_name', 'Sample Campaign'), 
    ('campaign_name', 'Sample Campaign 2'), ('campaign_name', 'Sample Campaign 3'), 
    ('campaign_id', '900990999'), ('campaign_id', '9009909992'), ('campaign_id', '9009909993'), 
    ('kpi_name', 'none'), ('kpi_name', 'kpi_1'), ('kpi_name', 'kpi_2')])

Most answers / documentation I read suggest to add to_dict(request.form) or dict(request.form) to convert the request to a dictionary however did´t work

Thanks

cizario
  • 3,995
  • 3
  • 13
  • 27
Isaac Rivera
  • 101
  • 1
  • 10

1 Answers1

0

The request.form object is an immutable dictionary and has type of werkzeug.ImmutableMultiDict which has to_dict(flat=True) function.

refer to: https://tedboy.github.io/flask/generated/generated/werkzeug.ImmutableMultiDict.to_dict.html

ImmutableMultiDict.to_dict(flat=True)

Return the contents as regular dict. If flat is True the returned dict will only have the first item present, if flat is False all values will be returned as lists.

so to get all values you need to set flat parameter to False in request.form.to_dict(flat=False)

cizario
  • 3,995
  • 3
  • 13
  • 27