我用一个表单创建了一个视图,该表单应该通过用户电子邮件发送消息,但是我在编码方面存在问题。我的模板有一些基里尔字母,如果我尝试发送邮件,则会收到错误消息:
'ascii' codec can't encode characters in position 325-329: ordinal not in range(128)
如果我将模板中的文本更改为英文字母,则一切正常。
查看:
def index(request):
seos = SEO.objects.get(id__exact=1)
socs = Social_networks.objects.get(id__exact=1)
globs = globalapp.objects.get(id__exact=1)
index = Index.objects.get(id__exact=1)
form = ContactForm(request.POST)
formmm = ContactusForm(request.POST)
email = globalapp.objects.values_list('emailfb', flat=True).get(id=1)
if form.is_valid():
subject = form.cleaned_data['subject']
sender = form.cleaned_data['sender']
message = form.cleaned_data['message']
fille = form.cleaned_data['fille']
recepients = email
from_email, to = sender, recepients
html_content = loader.render_to_string('globalapp/chunks/email_tpl.html',
{'subject': subject, 'sender':sender, 'message':message, 'fille':fille})
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
msg.send()
if request.method == 'POST':
subject = request.POST['subject']
email = request.POST['sender']
message = request.POST['message']
form = 'Шапка'
post.objects.create(
subject = subject,
email = email,
message = message,
form = form
)
return HttpResponse('')
return render(request, 'globalapp/index.html', {'seos': seos,
'socs': socs,
'globs': globs,
'index': index,
'form': form,
'formmm': formmm })
模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<h2>Новое сообщение с сайта!</h2>
<p>Имя: {{ subject }}</p>
<p>Email: {{ sender }}</p>
<p>Сообщение: {{ message }}</p>
</html>
我已经尝试过将.endoce('utf-8')
与模板一起使用,但尚未获得任何结果。