0

我处于调试状态,我想使用 PrinceXML 将 HTML 页面呈现为 PDF。

在我的主要 HTML 中,我有:

{% 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 %}

我的 style.html 是生成 PDF 所需的 PrinceXML 信息:

@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
}

我的问题是:当我将样式包含到我已经 {% load staticfiles %} 的 HTML 中时,我是否需要在 style.html 中再次加载它?

我的猜测是肯定的,因为正如 Django 文档中所说,包含将使用我的主 html 的上下文呈现 style.html,但 staticfiles 库不是上下文的一部分。我对吗 ?

4

1 回答 1

1

来自Django 文档

包含标签应被视为“渲染此子模板并包含 HTML”的实现,而不是“解析此子模板并包含其内容,就好像它是父模板的一部分一样”。这意味着包含的模板之间没有共享状态——每个包含都是一个完全独立的渲染过程。

所以,是的,您包含的模板不知道您的主 HTML 中发生了什么,因此您也应该包含staticfiles在您的包含模板中。

于 2014-10-02T06:07:24.870 回答