Django.fun

Django dict(request.POST.lists()) returns twice(normal, empty)

Views.py

@login_required(login_url="/users/login")
def tresults(request):
    ids = dict(request.POST.lists())
    print(ids)
    data = ids['selected']
    msg = data.pop(0)

HTML

<form id = "{{ msg }}" action="tresults" method="POST">
             <input type="hidden" name="selected" value="{{ msg }}">
             <div class="table-responsive">
                 <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                     <thead>
                         <tr>
                             <th>GuildName</th>
                             <th>GuildID</th>
                             <th>Check</th>
                         </tr>
                     </thead>
                     <tbody>

                            {% for guild in guilds %}
                                <tr>
                                    <td>{{ guild.name }}</td>
                                    <td>{{ guild.id }}</td>
                                    <td>
                                           {% csrf_token %}
                                           <input type="checkbox" name="selected" value="{{ guild.id }}"/>
                                    </td>
                                </tr>
                            {% endfor %}
                     </tbody>
                 </table>
             </div>
         </form>
         <a class="dropdown-item" href="#" onclick="document.forms['{{ msg }}'].submit();">Send</a>

and I getting from print(ids)

{'selected': ['61660a3afe554cfd1b4fe98f', '880716169986859039'], 'dataTable_length': ['10'], 'csrfmiddlewaretoken': ['OE9lhwbkU1KlKrDHiip1G6Yd5i9oOPS1bA0s2DapHY6RDbXc7UHc4KPd5jOlCLNm']}
[13/Oct/2021 07:55:57] "POST /posts/post/tresults HTTP/1.1" 302 0
{}
Internal Server Error: /posts/post/tresults

I tried with various way like

request.POST.dict() <- returns with only one value

request.POST.copy() and dict(ids.iterlists()) or dict(ids) <- not working

I don't know why two dict printed and last one is empty. I want to fix this

Answers: 1

Answered by Marcus Aldrey, Oct. 13, 2021, 5:42 a.m.

You have

<input type="hidden" name="selected" value="{{ msg }}">

and

<input type="checkbox" name="selected" value="{{ guild.id }}"/>

both with name="selected", change the name of one of then