DETAIL:失败行包含(9,null,miarisoa00@yahoo.de,,null)。
IntegrityError at /contact/contact/
null value in column "name" of relation "contact_contact" violates not-null constraint
DETAIL: Failing row contains (9, null, miarisoa00@yahoo.de, , null).
Request Method: POST
Request URL: http://127.0.0.1:8000/contact/contact/
Django Version: 3.2.15
Exception Type: IntegrityError
Exception Value:
null value in column "name" of relation "contact_contact" violates not-null constraint
DETAIL: Failing row contains (9, null, miarisoa00@yahoo.de, , null).
Exception Location: D:\MKSD_SITE\MKSD_WEB\site\lib\site-packages\django\db\backends\utils.py, line 84, in _execute
Python Executable: D:\MKSD_SITE\MKSD_WEB\site\Scripts\python.exe
Python Version: 3.9.13
Python Path:
['D:\\MKSD_SITE\\MKSD_WEB\\site\\src',
'C:\\Program Files\\Python39\\python39.zip',
'C:\\Program Files\\Python39\\DLLs',
'C:\\Program Files\\Python39\\lib',
'C:\\Program Files\\Python39',
'D:\\MKSD_SITE\\MKSD_WEB\\site',
'D:\\MKSD_SITE\\MKSD_WEB\\site\\lib\\site-packages']
Server time: Sun, 04 Jun 2023 16:39:42 +0000
views.py
def Kontact(request):
if request.method == 'POST':
contact = Contact()
message_name = request.POST.get('message-name')
from_email = request.POST.get('from-email')
ville = request.POST.get('ville')
message = request.POST.get('message')
contact.name = message_name
contact.email = from_email
ville = ville
contact.subject = message
contact.save()
message = f"""
Name: {message_name}
From: {from_email}
Stadt: {ville}
New message: \n\n{message}
"""
# Send email
send_mail(
message_name, #subject
message, #message
from_email, #from email
['kontakt.mksd@gmail.com'], #to email
fail_silently=False,
)
return render(request, 'contact.html', {'message_name': message_name})
else:
return render(request, 'contact.html', {})
models.py
class Contact(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(max_length=50)
ville = models.CharField(max_length=100)
subject = models.TextField(max_length=355)
def __str__(self):
return self.name
模板- HTML
<div class="row">
<form method="post" class="was-validated">
{% csrf_token %}
<div class="col-lg-6">Email (erforderlich):
<input type="email" name="from-email" class="form-control mb-30" placeholder="Your Email" required>
</div>
<div class="col-lg-6">Phone
<input type="phone" name="phone" class="form-control mb-30" placeholder="yours">
</div>
<div class="col-lg-6">Name (erforderlich):
<input type="name" name="name" class="form-control mb-30" placeholder="Name" required>
</div>
<div class="col-lg-6">Stadt
<input type="stadt" name="stadt" class="form-control mb-30" placeholder=" City">
</div>
<br><br><br>
<div class="col-12">
<textarea name="text" class="form-control mb-30" placeholder="text">
</textarea>
</div>
<div class="col-12 my-3">
<button type="submit" class="btn btn-success">
Send
</button>
</div>
</form>
</div>
1条答案
按热度按时间332nm8kg1#
输入元素的名称是
name
,而不是message-name
,与HTML表单中的ville
(stadt
)和message
(text
)相同,因此您用途:同时使用多个(自然)语言通常是一个坏主意。你似乎是英语,德国人和法国人的一部分,使这一切非常混乱。
注意:请使用
request.POST['ville']
,而不是request.POST.get('ville')
。使用.get(…)
通常只能隐藏错误,而不能隐藏问题:实际上,如果密钥丢失,它将工作。但如果确实需要该键的值,则只会在稍后的过程中导致更多的麻烦。注意:最好使用
Form
[Django-doc],而不是手动进行数据校验和清洗。Form
不仅可以简化HTML中表单的渲染,还可以更方便地验证输入,并将数据清理为更方便的类型。注意:如果POST请求成功,需要创建**
redirect
**[Django-doc]来实现Post/Redirect/Get pattern [wiki]。这样可以避免在用户刷新浏览器时发出相同的POST请求。