Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use french letters in a django template?

I have some french letters (é, è, à...) in a django template but when it is loaded by django, an UnicodeDecodeError exception is raised.

If I don't load the template but directly use a python string. It works ok.

Is there something to do to use unicode with django template?

like image 952
luc Avatar asked Jun 30 '09 13:06

luc


2 Answers

You are probably storing the template in a non-unicode encoding, such as latin-1. I believe Django assumes that templates are in UTF-8 by default (though there is a setting to override this).

Your editor should be capable of saving the template file in the UTF-8 encoding (probably via a dropdown on the save as page, though this may depend on your editor). Re-save the file as UTF-8, and the error should go away.

like image 186
Brian Avatar answered Sep 28 '22 07:09

Brian


This is from the Django unicode documentation related to your problem:

" But the common case is to read templates from the filesystem, and this creates a slight complication: not all filesystems store their data encoded as UTF-8. If your template files are not stored with a UTF-8 encoding, set the FILE_CHARSET setting to the encoding of the files on disk. When Django reads in a template file, it will convert the data from this encoding to Unicode. (FILE_CHARSET is set to 'utf-8' by default.)

The DEFAULT_CHARSET setting controls the encoding of rendered templates. This is set to UTF-8 by default. "

like image 20
Andre Miller Avatar answered Sep 28 '22 05:09

Andre Miller