我正在尝试使用Django来设置电子邮件和简单的表单,但它不起作用。我该如何修复错误?
蟒== 3.5.1 Django的== 1.8.8
抛出以下错误:
TypeError at /contact
not all arguments converted during string formatting
Request Method: POST
Request URL: http://127.0.0.1:8000/contact
Django Version: 1.8.8
Exception Type: TypeError
Exception Value:
not all arguments converted during string formatting
Exception Location: views.py in contact, line 60
这是views.py:
def contact(request):
form = ContactForm(request.POST or None)
if form.is_valid():
form_email = form.cleaned_data.get('email')
form_message = form.cleaned_data.get('message')
form_full_name = form.cleaned_data.get('full_name')
subject = 'お問い合わせ'
from_email = settings.EMAIL_HOST_USER
to_email = [from_email, 'me@email.com']
contact_message = """
%s: テスト
""" % (
form_full_name,
form_message,
form_email) #<-------error in this line
send_mail(subject,
contact_message,
from_email,
[to_email],
fail_silently=False)
context = {
"form": form,
}
return render(request, "forms.html", context)
答案 0 :(得分:1)
更改此行:
contact_message = """
%s: テスト
""" % (
form_full_name,
form_message,
form_email)
到
contact_message = """
%s: テスト, %s, %s
""" % (
form_full_name,
form_message,
form_email)
它是一个string formatting问题。字符串中%s
的数量必须与参数数量匹配。