Ошибка при отправке пустых форм в Django - PullRequest
0 голосов
/ 25 января 2020

У меня есть веб-страница с двумя формами, form1 и form2. При отправке пустых форм я получаю ошибки 1) MultiValueDictKeyError в / profile / adminKaLogin / 2) недопустимый литерал для int () с основанием 10 для form1 и form2 соответственно. Ниже приведены содержимое файла HTML и views.py:

HTML:

<div> 
        <h2>Check the boxes below to display records!</h2>
        <form method="POST" action="/profiles/adminKaLogin/">
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInline1" name="inlineDefaultRadiosExample" value="1">
                <label class="custom-control-label" for="defaultInline1">Vendor 1</label>
            </div>

            <!-- Default inline 2-->
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInline2" name="inlineDefaultRadiosExample" value="2">
                <label class="custom-control-label" for="defaultInline2">Vendor 2</label>
            </div>
        <br>  
        <h3> Select time period</h3>

            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines1" name="inlineDefaultRadiosExample1" value="1">
                <label class="custom-control-label" for="defaultInlines1">1 Month</label>
            </div>  
            <!-- Default inline 2-->
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines2" name="inlineDefaultRadiosExample1" value="2">
                <label class="custom-control-label" for="defaultInlines2">2 Months</label>
            </div>
            <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" id="defaultInlines3" name="inlineDefaultRadiosExample1" value="6">
                <label class="custom-control-label" for="defaultInlines3">6 Months</label>
            </div>
            <br>
            <button type="submit" class="btn btn-primary" name="form1">Submit</button>
            {% if model %}
            <table>
                <thead>
                    <tr>
                        <th style = "padding: 20px;">Vendor ID</th>
                        <th style = "padding: 20px;">Employee ID</th>
                        <th style = "padding: 20px;">Debit</th>
                        <th style = "padding: 20px;">Credit</th>
                        <th style = "padding: 20px;">Time of transaction</th>
                    </tr>
                </thead>
                <tbody>
                {% for i in model %}
                    {% for j in i %}
                    <tr>
                        <td style="text-align: center;">{{ j.vendor_id.id }}</td>
                        <td style="text-align: center;">{{ j.emp_id.id }}</td>
                        <td style="text-align: center;">{{ j.debit }}</td>
                        <td style="text-align: center;">{{ j.credit }}</td>
                        <td style="text-align: center;">{{ j.timestamp }}</td>
                    </tr>
                    {% endfor %}
                {% endfor %}
                </tbody>
            </table>
            {% else %}
                <p>There are no active transactions!</p>
            {% endif %}
        </form>

        <br>

    </div>
    <div>
        <h3>Want to update users' balance? Click below!</h3>
        <input type="button" name="answer" class="btn btn-primary" value="Click here!" onclick="showDiv()"/>
        <br>    
        <div id="welcomeDiv"  style="display:none;" class="answer_list" >
            <form method="POST" action="/profiles/adminKaLogin/">
              <input type="" class="form-control" id="amount1" name="amt" aria-describedby="emailHelp" placeholder="Enter amount"style="width: 25%;margin: 0 auto">
              <br>
              <button type="submit" class="btn btn-primary" name="form2">Submit</button>
            </form>
        </div>
    </div>

Views.py:

def adminKaPage(request):
    if request.method=="POST":
        if 'form1' in request.POST:
            d=[]
            vendor_choice = request.POST["inlineDefaultRadiosExample"]
            date_choice = request.POST["inlineDefaultRadiosExample1"]
            x = employee.objects.all()
            count=0
            for i in x:
                if date_choice == 1:
                    d.append(transaction.objects.filter(vendor_id=vendor_choice, emp_id = i.id, timestamp__gte = datetime.date.today() - datetime.timedelta(days=30)))
                    count+=1
                elif date_choice == 2:
                    d.append(transaction.objects.filter(vendor_id=vendor_choice, emp_id = i.id, timestamp__gte = datetime.date.today() - datetime.timedelta(days=60)))
                    count+=1
                else:
                    d.append(transaction.objects.filter(vendor_id=vendor_choice, emp_id = i.id, timestamp__gte = datetime.date.today() - datetime.timedelta(days=180)))
                    count+=1    
            return render(request, 'profiles/adminKaLogin.html', {'model':d})        


        if 'form2' in request.POST:
            amount = request.POST["amt"]
            x = employee.objects.all()
            y = vendor.objects.get(id=100)
            for i in x:
                i.balance = i.balance + int(amount)
                i.save()
                print(i.id)
                transaction.objects.create(vendor_id=y,emp_id=i,debit=0,credit=amount)

            return render(request, 'profiles/adminKaLogin.html')

    return render(request, 'profiles/adminKaLogin.html')

Имя формы (form1 и form2) были определены в их кнопках отправки. В файле views.py у меня есть 2 сегмента, по одному для каждой из двух форм.

1 Ответ

1 голос
/ 25 января 2020

изменить почтовый запрос на request.POST.get("inlineDefaultRadiosExample") и подобные места

...