提交表单后 Django 没有更新我的数据库
Django is not updating my database after submitting my form
我已经创建了一个模型和一个表单,两者都正常工作,并且我已经使用管理模块向数据库添加了数据
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 %}
一切似乎都正常,我可以在网站上提交数据,我收到一条消息,说明提交正常,但是,当我检查管理网站和检查数据库时,我看不到添加了新数据,本质上表单没有提交数据,我没有收到错误
[10/Jul/2021 18:36:24] "获取 /account/add_client/ HTTP/1.1" 200 12385
[10/Jul/2021 18:36:36] "POST /account/add_client/ HTTP/1.1" 200 12616
有谁知道我做错了什么吗?
我注意到我的错误在表格中,我不应该在实例中使用 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})
我已经创建了一个模型和一个表单,两者都正常工作,并且我已经使用管理模块向数据库添加了数据
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 %}
一切似乎都正常,我可以在网站上提交数据,我收到一条消息,说明提交正常,但是,当我检查管理网站和检查数据库时,我看不到添加了新数据,本质上表单没有提交数据,我没有收到错误
[10/Jul/2021 18:36:24] "获取 /account/add_client/ HTTP/1.1" 200 12385 [10/Jul/2021 18:36:36] "POST /account/add_client/ HTTP/1.1" 200 12616
有谁知道我做错了什么吗?
我注意到我的错误在表格中,我不应该在实例中使用 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})