表单未保存

2024-07-04 16:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在不验证的情况下保存django表单。我有一个简单的表格。就像我有一个下拉选项,如果用户选择一个figi它带来了一个不同的领域从模型支付,如果用户选择银行出纳员它带来了一个不同的领域从模型支付。所以我想救他们。但当我使用表格.u有效吗()并且它仍然向我显示代码post 200,但它不在我的数据库中,当我删除它时表格.u有效吗(),它抛出v错误:验证错误。现在imagefield不保存

class payments(models.Model):
    Amount=models.IntegerField(default=00000)
    figiID=models.CharField(default='F-00000',max_length=10,required=False)
    Bank_Teller=models.ImageField(upload_to='media/',required=False)

html格式

  <select id='modePayment'>
            <option value='test1'>Figi</option>
            <option value='test2'>Bank Teller</option>
            </select><br><br>
            <div class="test1 pricebox">
            <form method="post">
            {% csrf_token %}
              <h6>Amount:{{pDetail.Amount}}</h6>
              <h6>Figi-ID{{pDetail.figiID}}</h6>
              <button style="background:#4CAF50;color:white;width:150px;height:40px;" value="submit" >Invest</button>
            </form>
            </div>
            <div class="test2 pricebox">
            <form method="post" enctype="multipart/form-data">
            {% csrf_token %}
              <h6>Amount:{{pDetail.Amount}}</h6>
              <h6>Bank Teller{{pDetail.Bank_Teller}}</h6>
              <button style="background:#4CAF50;color:white;width:150px;height:40px;" value="submit" >Invest</button>
            </form>
            </div>

你知道吗视图.py你知道吗

def dashboard(request):
     if request.user.is_authenticated:
          allDocs = Registration.objects.all()
          pDetail=payment_form()
          if request.method=='POST':
                pDetail=payment_form(request.POST,request.FILES)
                if pDetail.is_valid():
                     pDetail.save()
        context={'doc':allDocs,'pDetail':pDetail,'iDetail':investment}
         return render(request,'dashboard.html',context)
  else:
       return redirect('Tan:login')

Tags: divformvaluemodelsrequestbuttonpostamount
1条回答
网友
1楼 · 发布于 2024-07-04 16:55:20

您忘记保存表单变量pDetail。你知道吗

Views.py

def dashboard(request):
     if request.user.is_authenticated:
          allDocs = Registration.objects.all()
          pDetail=payment_form()
          if request.method=='POST':
                pDetail=payment_form(request.POST,request.FILES)
                if pDetail.is_valid():
                pDetail=pDetail.save()
                pDetail.save()                #<  - Add this
         context={'doc':allDocs,'pDetail':pDetail,'iDetail':investment}
         return render(request,'dashboard.html',context)
  else:
       return redirect('Tan:login')

相关问题 更多 >

    热门问题