2014-10-02 25 views
0

我在調試狀態,我想呈現一個HTML頁面PrinceXML爲PDF。我需要加載包含的HTML staticfiles在Django?

在我的主要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中時,是否需要使用lo在style.html中再次添加廣告?

我的猜測是肯定的,因爲在Django文檔正如所說,包括將呈現與我的主要HTML的背景下style.html,但staticfiles庫不是上下文的一部分。我對嗎 ?

回答

1

Django docs

的包括標籤應被視爲實現「渲染 這個子模板,幷包括HTML」,而不是「分析此子模板 ,包括它的內容就好像它是父母的一部分「。這意味着 存在包含的模板之間沒有共享的狀態 - 每個 包括是完全獨立的渲染過程。

因此,您的收錄模板並不知道您的主HTML中發生了什麼,所以您應該在您的收錄模板中包含staticfiles

+0

我的文檔閱讀,但它並沒有回答以下問題:共享所包括的HTML的唯一的事情是上下文變量? – Laurent 2014-10-02 06:22:02

+1

沒錯。 :) – Filly 2014-10-02 06:30:15

+0

謝謝菲莉! – Laurent 2014-10-02 06:32:09

相關問題