最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Django form won't send post request - Stack Overflow

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论