Reputation: 47
I'm writing a script for converting a json file with html, into a rendered pdf. I wrote this:
from weasyprint import HTML
from django.template import Template, Context
from django.template.loader import render_to_string, get_template
import json
context = json.load(open("source path"))
rendered_string = render_to_string("template.html", context)
HTML(string=rendered_string).write_pdf("sample.pdf")
The problem is the output, all the html in the json is formatted as simple text and in bond type... the json part is ok. Someone knows how can i solve this?
this is the template:
{%for elemento in elementi%}
{%if not elemento.tipo%}
<h1>{{elemento.html}}</h1>
{%else%}
<ul>
{%for f in elemento.form %}
<li>{{f}}</li>
{%endfor%}
</ul>
{%endif%}
{%endfor%}
and this is the json:
{"elementi":[{"id":107729,"titolo":null,"tipo":false,"form":null,"html":"<span lessico='Questa' idx=\"0\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">Questa</span> <span lessico='è' idx=\"1\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">è</span> <span lessico='una' idx=\"2\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">una</span> <span lessico='domanda' idx=\"3\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">domanda</span>...","traduci":[],"inputLessicale":[],"esercizioId":101624,"italiano":null,"testo_principale":"Questa è una domanda...","testo_principale_pre":"","testo_principale_post":"","risposta_1":"","risposta_2":"","risposta_3":"","risposta_4":"","distrattore_1":"","distrattore_2":"","distrattore_3":"","distrattore_4":"","parole_gia_ricostruite":"","continua":false,"focus":true,"ignora_lessico":false,"etichetta_1":null,"etichetta_2":null,"etichetta_3":null,"etichetta_4":null,"etichetta_5":null},
{"id":107730,"titolo":"Scegli la risposta corretta","tipo":"M","form":["a scelta multipla","con risposta aperta","di tipo trova","di associazione"],"html":null,"traduci":[],"inputLessicale":[],"esercizioId":101624,"italiano":null,"testo_principale":"a scelta multipla","testo_principale_pre":"","testo_principale_post":"","risposta_1":"","risposta_2":"","risposta_3":"","risposta_4":"","distrattore_1":"con risposta aperta","distrattore_2":"di associazione","distrattore_3":"di tipo trova","distrattore_4":"","parole_gia_ricostruite":"","continua":false,"focus":false,"ignora_lessico":false,"etichetta_1":null,"etichetta_2":null,"etichetta_3":null,"etichetta_4":null,"etichetta_5":null}],"ordine_in_verifica":null}
Upvotes: 0
Views: 3599
Reputation: 2348
It's a safety feature of Django to avoid HTML injection, see this answer from a similar thread:
https://stackoverflow.com/a/4848661/1047040
Instead of:
{{ elemento.html }}
Use this:
{{ elemento.html | safe }}
From the Django docs:
Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.
Link: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe
Upvotes: 1