I want to use weasyprint to transform a html template to PDF. I'm using Django 5.0.
Here is a part of my template (The template works fine, Django renders it correctly):
<div>
<h1>Tu Pedido está listo, {{request.user.username}}!</h1>
</div>
<h4>Pedido ID: {{pedido}}</h3>
<div class="container text-center" >
<table class="table">
<thead class="thead-dark">
<tr>
<th>Producto</th>
<th>Cantidad</th>
<th>Precio</th>
</tr>
</thead>
<tbody>
{% for key, producto in carro.items %}
<tr>
<td>
{{producto.nombre}}
</td>
<td>
{{producto.cantidad}}
</td>
<td>
$: {{producto.precio}}
</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">Total: $: {{total}}</td>
</tr>
</tbody>
</table>
</div>
<div class="container text-center">
<a href="{% url 'home' %}" class="btn btn-outline-primary me-2">Volver</a>
<!--pdf-->
<form id="pdf_form" method="post" action="{% url 'generar_pdf' %}" style="display: none;">
{% csrf_token %}
<input type="hidden" name="pedido" value="{{ pedido }}">
<input type="hidden" name="carro" value="{{ carro }}">
<input type="hidden" name="total" value="{{ total }}">
</form>
<button id="pdf_button" class="btn btn-primary" type="submit">Descargar pdf</button>
</div>
<!-- send form -->
<script>
document.getElementById("pdf_button").addEventListener("click", function() {
document.getElementById("pdf_form").submit();
});
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
And here is my views.py:
def generar_pdf(request):
if request.method == "POST":
pedido = request.POST.get("pedido")
carro_info = request.POST.get("carro")
total_compra = request.POST.get("total")
# if I print these 3 variables, the values are ok
#--- here is the problem ---#
html_string = render_to_string(
template_name='exito.html',
context={"pedido": pedido, "carro":carro_info, "total": total_compra},
request=request)
#------#
html = HTML(string=html_string)
pdf_file = html.write_pdf()
response = HttpResponse(pdf_file, content_type='pdf')
response['Content-Disposition'] = 'attachment; filename="mi_archivo.pdf"'
return response
- context:
pedido: an instance of a model called Pedidocarro_info: a dictionary: e.g. {'1': {'id_producto': 1, 'nombre': 'Destornillador', 'precio': 12.1, 'cantidad': 1, 'imagen': '/media/tienda/destornillador.png'}}total_compra: a float
html_string is a string of the html template, but not considering these lines:
<tbody>
{% for key, producto in carro.items %}
<tr>
<td>
{{producto.nombre}}
</td>
<td>
{{producto.cantidad}}
</td>
<td>
$: {{producto.precio}}
</td>
</tr>
{% endfor %}
<tr>
<td colspan="3">Total: $: {{total}}</td>
</tr>
</tbody>
The cause of this is because render_to_string() can't access to the items of the carro dictionary.
I thought it was a problem of the {% for %} statement or maybe just the dictionary. However, when I try to print in the template something like: {{pedido.id}}, render_to_string() completely ignores the statement.
What can I do? Should I use other django functions to transform the template to string? Thanks in advance.
