简体   繁体   English

我是否需要在Django随附的HTML中加载静态文件?

[英]Do I need to load staticfiles in included HTML in Django?

I'm in DEBUG state and I want to render an HTML page to PDF with PrinceXML. 我处于DEBUG状态,我想使用PrinceXML将HTML页面呈现为PDF。

In my main HTML I have : 在我的主要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 %}

My style.html is the princeXML information needed to generate the PDF : 我的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
}

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 ? 我的问题是:当我将样式包含到已经{%load staticfiles%}的HTML中时,是否需要在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. 我的猜测是肯定的,因为正如Django文档中所述,include将使用我的主要html上下文呈现style.html,但是staticfiles库不是上下文的一部分。 Am I right ? 我对吗 ?

From the Django docs : Django文档中

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". include标记应被视为“呈现此子模板并包含HTML”的实现,而不应视为“解析此子模板并像其父对象一样包含其内容”。 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. 因此,是的,您的包含模板不知道您的主要HTML中发生了什么,因此您也应该在包含的模板中包含staticfiles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM