from django import forms
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
from . import models
#使用modelform
class UserInfoForm(forms.ModelForm):
#xxx=forms.CharField(max_length=11)#自定义字段
class Meta:
model = models.UserInfo
fields = ['name','age','shijian','bumen']
def __init__(self,*args,**kwargs):
super(UserInfoForm,self).__init__(*args,**kwargs)
for name,filed in self.files.items():
if name=='password':
continue
filed.widget.attrs={"class","form-control"}
def index(request):
if request.method=='get':
form=UserInfoForm()
return render(request,'userinfoform.html',{"form":form})
elif request.method=='post':
form=UserInfoForm(request.POST)
if form.is_valid():#利用modelform进行数据校验。
form.save()
return redirect('index')
用modelform进行数据校验并保存
最新推荐文章于 2024-09-05 09:40:33 发布