I have a contact form
class ContactForm(forms.Form):
name = forms.CharField(required=True, max_length=100, label=_('firstname'))
mail = forms.EmailField(required=True, max_length=150, label=_('mail'))
def display(request):
form = ContactForm(request.POST)
if form.is_valid():
...
else:
form = ContactForm()
return render_to_response('contact.html', {'contact_form' : form}, context_instance=RequestContext(request))
With from django.utils.translation import gettext_lazy as _
In my template contact.html
<form action="#" method="post" >{% csrf_token %}
{{ contact_form.as_p }}
<input type="submit" value="Send" />
</form>
I have this error
'ascii' codec can't encode character u'\\xe9' in position 2: ordinal not in range(128)
Unicode error hint
The string that could not be encoded/decoded was: Prénom
The problem is _('firstname'))
. But it works when
.encode('utf8')
at the end it's solved. _('other string')
_('firstname')+''
{% trans "firstname" %}
But I don't want to do this in all attributs of my contact form (which contains more attributs).
How can I do that for name and email both ?
Thanks
解决办法是更换gettext_lazy
由ugettext_lazy
因为字符串Prénom
包含一个特殊的Unicode信é
Type on the first line:
# -*- coding: utf-8 -*-
Python recognises this line as instructions of encoding type for your entire file.
You don't specify whether you use ModelForms or not, but can you check in your template that your browser will correctly encode the submitted data with the content='text/html; charset=UTF-8'
content='text/html; charset=UTF-8'
attribute in the form
tag? Like:
<form action='/someurl' method='post' content='text/html; charset=UTF-8'>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.