Django email template encoding











up vote
0
down vote

favorite












I maked a view with a form, that should send message on user email, but i have a problev with encoding. My template have some kirillic letters, and if i try to send mail i get an error:



'ascii' codec can't encode characters in position 325-329: ordinal not in range(128) 


If i change text in my template to english letters everything works.



View:



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 })


Template:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<h2>Новое сообщение с сайта!</h2>

<p>Имя: {{ subject }}</p>
<p>Email: {{ sender }}</p>
<p>Сообщение: {{ message }}</p>
</html>


Ill tryed already to use .endoce('utf-8') with template, but didnt get any results yet.










share|improve this question






















  • Please indicate on which line the error occurs.
    – dirkgroten
    Nov 19 at 13:44










  • On line msg.send()
    – Andrej Vilenskij
    Nov 19 at 13:46










  • if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
    – dirkgroten
    Nov 19 at 13:52










  • >>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
    – Andrej Vilenskij
    Nov 19 at 13:57






  • 1




    You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
    – dirkgroten
    Nov 19 at 14:27















up vote
0
down vote

favorite












I maked a view with a form, that should send message on user email, but i have a problev with encoding. My template have some kirillic letters, and if i try to send mail i get an error:



'ascii' codec can't encode characters in position 325-329: ordinal not in range(128) 


If i change text in my template to english letters everything works.



View:



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 })


Template:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<h2>Новое сообщение с сайта!</h2>

<p>Имя: {{ subject }}</p>
<p>Email: {{ sender }}</p>
<p>Сообщение: {{ message }}</p>
</html>


Ill tryed already to use .endoce('utf-8') with template, but didnt get any results yet.










share|improve this question






















  • Please indicate on which line the error occurs.
    – dirkgroten
    Nov 19 at 13:44










  • On line msg.send()
    – Andrej Vilenskij
    Nov 19 at 13:46










  • if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
    – dirkgroten
    Nov 19 at 13:52










  • >>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
    – Andrej Vilenskij
    Nov 19 at 13:57






  • 1




    You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
    – dirkgroten
    Nov 19 at 14:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I maked a view with a form, that should send message on user email, but i have a problev with encoding. My template have some kirillic letters, and if i try to send mail i get an error:



'ascii' codec can't encode characters in position 325-329: ordinal not in range(128) 


If i change text in my template to english letters everything works.



View:



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 })


Template:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<h2>Новое сообщение с сайта!</h2>

<p>Имя: {{ subject }}</p>
<p>Email: {{ sender }}</p>
<p>Сообщение: {{ message }}</p>
</html>


Ill tryed already to use .endoce('utf-8') with template, but didnt get any results yet.










share|improve this question













I maked a view with a form, that should send message on user email, but i have a problev with encoding. My template have some kirillic letters, and if i try to send mail i get an error:



'ascii' codec can't encode characters in position 325-329: ordinal not in range(128) 


If i change text in my template to english letters everything works.



View:



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 })


Template:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<h2>Новое сообщение с сайта!</h2>

<p>Имя: {{ subject }}</p>
<p>Email: {{ sender }}</p>
<p>Сообщение: {{ message }}</p>
</html>


Ill tryed already to use .endoce('utf-8') with template, but didnt get any results yet.







django python-3.x django-forms django-templates django-views






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 13:22









Andrej Vilenskij

49110




49110












  • Please indicate on which line the error occurs.
    – dirkgroten
    Nov 19 at 13:44










  • On line msg.send()
    – Andrej Vilenskij
    Nov 19 at 13:46










  • if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
    – dirkgroten
    Nov 19 at 13:52










  • >>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
    – Andrej Vilenskij
    Nov 19 at 13:57






  • 1




    You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
    – dirkgroten
    Nov 19 at 14:27


















  • Please indicate on which line the error occurs.
    – dirkgroten
    Nov 19 at 13:44










  • On line msg.send()
    – Andrej Vilenskij
    Nov 19 at 13:46










  • if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
    – dirkgroten
    Nov 19 at 13:52










  • >>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
    – Andrej Vilenskij
    Nov 19 at 13:57






  • 1




    You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
    – dirkgroten
    Nov 19 at 14:27
















Please indicate on which line the error occurs.
– dirkgroten
Nov 19 at 13:44




Please indicate on which line the error occurs.
– dirkgroten
Nov 19 at 13:44












On line msg.send()
– Andrej Vilenskij
Nov 19 at 13:46




On line msg.send()
– Andrej Vilenskij
Nov 19 at 13:46












if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
– dirkgroten
Nov 19 at 13:52




if you open a django shell, from django.conf import settings and print settings.DEFAULT_CHARSET what do you get?
– dirkgroten
Nov 19 at 13:52












>>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
– Andrej Vilenskij
Nov 19 at 13:57




>>> from django.conf import settings >>> settings.DEFAULT_CHARSET 'utf-8'
– Andrej Vilenskij
Nov 19 at 13:57




1




1




You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
– dirkgroten
Nov 19 at 14:27




You're using the console email backend, maybe your console doesn't allow non-ascii output? Try using a proper email backend as you would in production (e.g. SMTP) and see if that works. If it does, then maybe change your code to differentiate between backends.
– dirkgroten
Nov 19 at 14:27

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375573%2fdjango-email-template-encoding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375573%2fdjango-email-template-encoding%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga