Django form won't send post request
Hello I'm rebuilding my inventory application with the use of django forms and jinja template syntax I can't get the form to send a post request. here's my form in the template
{% extends "inventory/base.html" %} {% block content%}
<form action="inventory/stock/{{part_id}}" method="post">{% csrf_token %} {{form}}</form>
<button type="submit" name="confirm" >Confirm</button>
<button type="submit" name="cancel">Cancel</button>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
here's my URL:
path('stock/<int:part_id>/', views.add_stock_page, name='add_stock')
here's my View:
@login_required
def add_stock_page(request, part_id):
print('hi')
template= loader.get_template('inventory/addStock.html')
user= request.user
form = StockForm()
title = 'Add Stock'
if 'confirm' in request.POST:
print('confirm')
messages.info(request,'confirm')
if 'cancel' in request.POST:
messages.info(request,'cancel')
return render(request, 'inventory/addStock.html',{
'user':user,
'form':form,
'title':title,
'part_id':part_id
# 'messages':messages
})
here's my form:
class StockForm(forms.ModelForm):
quantity = forms.IntegerField(required=True)
price = forms.DecimalField(required=True)
class Meta:
model = Stock
fields = [
'supplier_id',
'brand_id',
'price'
]
labels ={
'quantity':'Quantity',
'supplier_id':'Supplier',
'brand_id':'Brand',
'price':'Price'
}
<form action="inventory/stock/{{part_id}}" method="post">{% csrf_token %} {{form}}</form>
<button type="submit" name="confirm" >Confirm</button>
<button type="submit" name="cancel">Cancel</button>
the submit buttons aren't inside the form tags
Try something like this:
<form action="inventory/stock/{{part_id}}" method="post">
{% csrf_token %}
{{form}}
<button type="submit" name="confirm" >Confirm</button>
<button type="submit" name="cancel">Cancel</button>
</form>