I'm in DEBUG state and I want to render an HTML page to PDF with PrinceXML.
In my main HTML I have :
{% extends "base.html" %}
{% load staticfiles %}
{% load url from future %}
{% block title %}Title{% endblock %}
{% block style %}
{% include "style.html" %}
<link rel="stylesheet" type="text/css" href="{% static "more.style.css" %}"/>
{% endblock %}
{% block branding %}<a class='brand' rel="nofollow" href="{% url 'url' %}">Brand</a>{% endblock %}
{% block userlinks %}
{% if user.is_authenticated %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{ user }}
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="{% url 'generate-pdf' %}">Get the doc in pdf</a></li>
<li><a href="{% url 'dashboard.views.index' %}">Home</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
</ul>
</li>
{% endif %}
{% endblock %}
My style.html is the princeXML information needed to generate the PDF :
@page {
margin-left: 0.8cm;
margin-right: 0.8cm;
margin-bottom: 2cm;
margin-top: 4cm;
@top-left {
margin-left: -0.6cm;
margin-right: -0.6cm;
content: url({% static "url" %});
}
@bottom-right {
border-top: solid 1px #bbb;
margin-top: 0.4cm;
vertical-align: middle;
font-size: 8pt;
content: counter(page) "/" counter(pages)
}
@bottom-center {
border-top: solid 1px #bbb;
margin-top: 0.4cm;
vertical-align: middle;
font-size: 8pt;
content: "{% now 'j.m.Y' %}"
}
@bottom-left {
border-top: solid 1px #bbb;
margin-top: 0.4cm;
padding-right: 2cm;
vertical-align: middle;
font-size: 8pt;
content: "footer info"
}
size: A4
}
html {
font-family: Arial, Verdana, Geneva, Helvetica, sans-serif ;
}
div.page-header {
page-break-before: always
}
My question is : As I include the style to a HTML where I already {% load staticfiles %}, do I need to load it again in the style.html ?
My guess is yes, because as said in Django docs, the include will render style.html with the context of my main html, but the staticfiles library is not part of the context. Am I right ?
From the Django docs:
The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.
So, yes, your included template doesn't know what's happening in your main HTML, so you should include staticfiles
in your included template as well.