How to properly bind a Django form?

I am working on a form that picks a Sale id and submits it to the server. When i submit it to the server a bind it using form = DeleteSalesForm(request.POST) and check it form.is_bound returns False. This is my

def delete_sales(request):
    if not request.user.is_authenticated:
        return redirect('/login')
    if request.method == 'GET':
        form = DeleteSalesForm()
        return render(request, 'inventorymanagment/delete_sales.html', {'form': form})
    form = DeleteSalesForm(request.POST)
    print("checking if form is valid", request.POST, form.is_bound, form)
    if form.is_valid():
        # redirect
        return redirect('/')
        print("sale not deleted")
        # why is the form invalid?
        return render(request, 'inventorymanagment/delete_sales.html', {'form': form})

Here is the traceback:

[31/Aug/2022 15:38:36] "POST /delete_sales HTTP/1.1" 200 932
checking if form is valid <QueryDict: {'csrfmiddlewaretoken': ['q3aga7bySuscQ6bb9zrh3pFUlfoIoRoGQnHvQQ8LlbiEfUl1XDSGp2fk3nCj5KJk'], 'id': ['16']}> False <tr>
    <th><label for="id_id">Sale:</label></th>

      <select name="id" id="id_id">
  <option value="16">id: 16, iphone 6 quantity: 50 price: 30.00 day: 2022-08-31</option>

  <option value="17">id: 17, Iphone 8 quantity: 70 price: 80.00 day: 2022-08-31</option>


sale not deleted

why isn't the form validating?

Well, you are currently overwriting the __init__ method of the form incorrectly.

Try passing in the *args and **kwargs as arguments to constructor method of the parent class.

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['id'] = forms.ChoiceField(
    label="Sale", choices=[(, i) for i in Sales.objects.all()], required=True)
Back to Top