# models.pyfrom django.db import modelsclass Listing(models.Model): business_name = models.CharFIEld(max_length=80) business_email = models.EmailFIEld() business_website = models.CharFIEld(max_length=80) business_phone = models.CharFIEld(max_length=80)# forms.pyfrom django import formsclass NewBusinessForm(forms.Form): business_name = forms.CharFIEld(label='Business name',max_length=100) business_phone = forms.CharFIEld(label='Phone Number',max_length=100) business_email = forms.EmailFIEld(label='Email Address',max_length=100) business_website = forms.CharFIEld(label='Web Site',max_length=100)# vIEws.pyfrom django.shortcuts import renderfrom django.http import httpResponseRedirectfrom .forms import NewBusinessFormdef new_business(request): if request.method == 'POST': form = NewBusinessForm(request.POST) if form.is_valID(): # process form data return httpResponseRedirect('/') else: form = NewBusinessForm() return render(request,'directory/new.HTML',{'form': form})解决方法 您需要创建对象并手动设置所有字段.这是一个例子.
def new_business(request): if request.method == 'POST': form = NewBusinessForm(request.POST) if form.is_valID(): # process form data obj = Listing() #gets new object obj.business_name = form.cleaned_data['business_name'] obj.business_email = form.cleaned_data['business_email'] obj.business_phone = form.cleaned_data['business_phone'] obj.business_website = form.cleaned_data['business_website'] #finally save the object in db obj.save() return httpResponseRedirect('/') ....
请注意,如果字段值不遵循约束,则保存对象可能会失败.所以你需要照顾它.
总结以上是内存溢出为你收集整理的python – 在Django中保存表单数据全部内容,希望文章能够帮你解决python – 在Django中保存表单数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)