Django form won't send post request - Stack Overflow

Hello I'm rebuilding my inventory application with the use of django forms and jinja template synt

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'
        }

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'
        }
Share Improve this question asked Mar 12 at 9:28 Jacob MutaleJacob Mutale 1491 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3
<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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744761833a4592219.html

相关推荐

  • Django form won&#39;t send post request - Stack Overflow

    Hello I'm rebuilding my inventory application with the use of django forms and jinja template synt

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信