为了账号安全,请及时绑定邮箱和手机立即绑定

我无法通过 Django 将表单内容保存在数据库中

我无法通过 Django 将表单内容保存在数据库中

哈士奇WWW 2023-07-11 10:51:41
大家好,我正在尝试在我的数据库中保存表单 my models.pyclass customer(models.Model):    customerid = models.CharField(default=str(uuid.uuid1()), max_length=500, primary_key=True)    customername=models.CharField(max_length=1000,)我的表单.pyfrom .models import customerclass createcustomerform(ModelForm):    class Meta:        model=customer        fields=['customername']我的观点.pydef createcustomer(request):    if request.method=='GET':        return render (request,'marketing/createcustomer.html',{'form':createcustomerform()})    else:        try:            form=createcustomerform(request.POST)            newcreatecustomer=form.save(commit=False)            newcreatecustomer.user=request.user            newcreatecustomer.save()            return redirect('createcustomer')        except ValueError:            return render(request,'marketing/createcustomer.html',{'form':createcustomerform(),'error':'Check for Invalid Data. Try Again.'})我的html是<form method="POST">    {% csrf_token %}    <div id="customernamename"> <span>Company Name</span>      <input type="text" id="customername" placeholder="Enter Company's Full name"> </div></form>当我单击“保存”按钮时,它再次将我重定向到“createcustomer”,但不将数据保存在我的数据库中。请帮助出什么问题或我错过了什么?它通过管理区域而不是 html 页面来保存客户。
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

你的错误在这里:newcreatecustomer.user=request.user

变量newcreatecustomer没有user字段;这就是它不起作用的原因。


查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信