Django template and latex

django, latex, templates

Solution

This is a result of automatic HTML escaping. As you are not generating HTML, you can turn this feature off:

{% autoescape off %}
    \documentclass[11pt]{lettre}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[francais]{babel}
    \usepackage{lmodern}
    ...

    \begin{document}
        \begin{letter}{...}
            \name{My name}
            \address{My address}
            \lieu{My city}
            ...
            \conc{ {{ title }} }
        ...
        \end{letter}
    \end{document}
{% endautoescape %}

Problem

I want to use Latex to create PDF files with my Django templates. But, when it generates the PDF file, some characters are not encoded in a good way, especially the ' (apostrophe): Latex stops compiling and if I ignore the error, the output is &#39. I'm French, so my Python files are UTF-8, my database UTF8-general-CI and I use those packages for Latex: [utf8]{inputenc} [T1]{fontenc} [francais]{babel} {lmodern} All french accents are generated, so I don't know where the problem is? Any idea? Here is my view: ``` # -*- coding: utf-8 -*- ... def results_contact(request, checkup_id): ... title = get_object_or_404(Checkup, pk=checkup_id).name ... template_file = 'activities/results.tex' t = loader.get_template(template_file) context = Context({ ... "title": title, ... }) ... f.write(smart_str(t.render(context))) ... return HttpResponseRedirect('/download_file/upload/checkups/%s.pdf' % basename) ``` And my template: ``` \documentclass[11pt]{lettre} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage[francais]{babel} \usepackage{lmodern} ... \begin{document} \begin{letter}{...} \name{My name} \address{My address} \lieu{My city} ... \conc{ {{ title }} } %here is my variable ... \end{letter} \end{document} ``` For example, if title=Visite d'Adam, the output will be Objet : Visite d&#39Adam Here is the log: ``` ! Misplaced alignment tab character &. <argument> Visite d& ##39;embauche l.35 \conc{ Visite d&#39;Adam } ? ```

Original source