我无法将数据存储在数据库中,当我单击按钮“验证”=表单无效时,出现错误消息
你知道吗型号.py你知道吗
from django.db import models
from django.contrib.auth.models import User
import datetime
class automobile(models.Model):
firstname = models.CharField(max_length=256)
lastname = models.CharField(max_length=256)
mail=models.CharField(max_length=256,default='NULL', blank = True,null=True)
investissement=models.IntegerField(default=0, blank = True,null=True)
telephone =models.IntegerField(blank = True, null =True)
def __unicode__(self):
return self.lastname + ' ' + self.firstname
你知道吗视图.py你知道吗
# -*- coding: utf-8 -*-
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from autoroute.models import automobile
from django import forms
from datetime import date
import datetime
class automobileF(forms.Form):
lastname = forms.CharField(label="Votre Nom (*)" )
firstname = forms.CharField(label="Votre Prénom(*)")
mail=forms.CharField(label = "Votre adresse mail (*)" )
investissement=forms.IntegerField(label = "Votre investissement (*)")
telephone =forms.IntegerField(label="votre numéro de téléphone (*)")
我的功能是这样的:
def automobile(request):
response={}
from automobile.views import automobileF
if request.method=='POST':
formset=automobileF(request.POST)
if formset.is_valid():
try:
contact=automobile.objects.get(lastname=form.cleaned_data['lastname'])
return HttpResponseRedirect('frontend/return_automobile.html')
except:
contact=automobile(lastname=form.cleaned_data['lastname'])
contact.firstname=form.cleaned_data['firstname']
contact.mail=form.cleaned_data['mail']
contact.telephone=form.cleaned_data['telephone']
contact.investissement=form.cleaned_data['investissement']
contact.save()
return HttpResponseRedirect('frontend/success_automobile.html')
else:
return HttpResponse('form invalid')
else:
return render (request,'frontend/automobile.html',response)
return render (request,'frontend/automobile.html',{'formset': formset})
你知道吗汽车.html你知道吗
<div class="row">
<div class="span10">
<form method="POST" role="form" action ="/automobile/"> {% csrf_token %}
<label style="color:#000000;margin-top:10px"><strong> Votre nom (*)</strong></label>
<input type="text" name="lastname" value="{{formset.lastname}}" >
<label style="color:#000000;margin-top:10px"><strong> Votre prenom (*)</strong></label>
<input type="text" name="firstname" value="{{formset.firstname}}" >
<label style="color:#000000;margin-top:10px"><strong> Votre adresse mail (*) </strong></label>
<input type="text" class="input-xlarge" name="address" value="{{formset.mail}}" >
<label style="color:#000000;margin-top:10px"><strong> investissement (*) </strong></label>
<input type="text" class="input-small" name="zip_code" value="{{formset.investissement}}" >
<label style="color:#000000;margin-top:10px"><strong> Numéro de telephone (*) </strong</label>
<input type="text" name="telephone" value="{{formset.telephone}}" >
<p style="text-align:center"> <button type="submit" class="btn btn-danger"> Validate</button> </p>
</form>
</div>
</div>
有人有办法解决这个问题
首先,请不要将表单变量称为“formset”:它不是一个formset,而是一个表单。你知道吗
第二,Django很好地验证了表单,但您不会将其发送回模板以显示任何错误。去掉第一个
else
子句,让执行一直到最后一行,这样表单就被发送到模板。然后,确保在模板中显示{{ formset.errors }}
,或者在字段旁边单独显示错误。你知道吗(另外,请学习如何在HTML中使用CSS类而不是内联样式。)
相关问题 更多 >
编程相关推荐