提交表单后,Django 没有更新我的数据库
Posted
技术标签:
【中文标题】提交表单后,Django 没有更新我的数据库【英文标题】:Django is not updating my database after submitting my form 【发布时间】:2021-09-20 17:31:37 【问题描述】:我已经创建了一个模型和一个表单,两者都正常工作,并且我已经使用管理模块将数据添加到数据库中
models.py
class Client(models.Model):
firstname = models.CharField(blank=True, max_length=30)
lastname = models.CharField(blank=True, max_length=15)
company = models.ForeignKey(Company, on_delete=models.CASCADE, default="company")
position = models.CharField(blank=True, max_length=15)
country = CountryField(blank_label='(select country)')
email = models.EmailField(blank=True, max_length=100, default="this_is@n_example.com")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
phone = PhoneField(default="(XX)-XXX-XXX")
def __str__(self):
return f'self.firstname'
forms.py
class ClientForm(forms.ModelForm):
class Meta:
model = Client
fields = ('firstname', 'lastname',"position",'country','email','phone')
views.py
@login_required
def add_client(request):
if request.method == "POST":
client_form = ClientForm(instance=request.user, data=request.POST)
if client_form.is_valid():
client_form.save()
messages.success(request, 'You have successfully added a client')
else:
messages.success(request, "Error Updating your form")
else:
client_form = ClientForm(instance=request.user)
return render(request,
"account/add_client.html",
'client_form':client_form)
add_client.html
% extends "base.html" %
% block title %Client Information % endblock %
% block content %
<h1> Client Form</h1>
<p>Please use the form below to add a new client to the database:</p>
<form method="post" enctype="multipart/form-data">
client_form.as_p
% csrf_token %
<p><input type="submit" value="Save changes"></p>
</form>
% endblock %
一切似乎都正常,我可以在网站上提交数据,我收到一条消息,说明提交正常,但是,当我检查管理网站并检查数据库时,我看不到新的添加了数据,基本上表单没有提交数据,我没有收到错误
[2021 年 7 月 10 日 18:36:24]“GET /account/add_client/HTTP/1.1”200 12385 [10/Jul/2021 18:36:36]“POST /account/add_client/HTTP/1.1”200 12616
有谁知道我做错了什么?
【问题讨论】:
尝试在您的视图中删除此行:else: messages.success(request, "Error Updating your form") 这样您就可以看到表单的错误消息。 感谢贡献,我做到了,没有错误:@login_required def add_client(request): if request.method == "POST": client_form = ClientForm(instance=request.user, data=request.POST) if client_form.is_valid(): client_form.save() messages.success(request, 'You have successfully added a client') #else: #messages.success(request, "Error Updating your form") else: client_form = ClientForm(instance=request.user) return render(request, "account/add_client.html", 'client_form':client_form)
【参考方案1】:
我注意到我的错误在表单中,我不应该在实例中使用 request.user,@login_required 就足够了。
请查看修改后的代码:
@login_required
def add_client(request):
if request.method == "POST":
client_form = ClientForm(request.POST)
if client_form.is_valid():
client_form.save()
messages.success(request, 'You have successfully added a client')
return HttpResponseRedirect(reverse('add_client'))
else:
messages.error(request, "Error Updating your form")
else:
client_form = ClientForm()
return render(request,
"account/add_client.html",
'client_form':client_form)
【讨论】:
以上是关于提交表单后,Django 没有更新我的数据库的主要内容,如果未能解决你的问题,请参考以下文章