如何在不进行验证的情况下保存Django表单。我有一个简单的表格。就像我有一个下拉选项,如果用户选择一个无花果,它将带来与模型支付不同的字段,如果用户选择银行柜员,它将带来与模型支付不同的字段。所以我想救他们。但是当我使用form.is_valid()时它并没有保存,它仍然向我显示代码发布200,但它不在我的数据库中,并且当我删除form.is_valid()时,它抛出valueError:validation错误。现在,图像字段不保存
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>
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.save()
context={'doc':allDocs,'pDetail':pDetail,'iDetail':investment}
return render(request,'dashboard.html',context)
else:
return redirect('Tan:login')
答案 0 :(得分:0)
您忘记保存表单变量 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')