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

大家好,我正在尝试在我的数据库中保存表单 my models.py


class customer(models.Model):

    customerid = models.CharField(default=str(uuid.uuid1()), max_length=500, primary_key=True)

    customername=models.CharField(max_length=1000,)


我的表单.py


from .models import customer


class createcustomerform(ModelForm):

    class Meta:

        model=customer

        fields=['customername']

我的观点.py


def 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 页面来保存客户。


哈士奇WWW
浏览 107回答 1
1回答

海绵宝宝撒

你的错误在这里:newcreatecustomer.user=request.user变量newcreatecustomer没有user字段;这就是它不起作用的原因。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python