【发布时间】:2017-08-09 11:55:48
【问题描述】:
我正在尝试使用 Django(1.8) render_to_string 通过管理命令将 html 转换为 pdf,而不是使用 View/request。以下代码能够将模板转换为 pdf。但它无法将 CSS 加载到模板中。
def html_to_pdf():
...
context = {'some_key': 'some_value'}
html = render_to_string('my_app/sample.html', context)
file.write(html)
...
示例.html
{% load static %}
<link href="{% static 'my_app/css/sample.css' %}" rel="stylesheet" type="text/css"/>
我还在设置文件中定义了静态:
STATIC_URL = '/static/'
在我的 Django 项目中,有另一个 View 函数可以在调用 url 时使用 CSS 加载模板。所以我认为这个问题与render_to_string有关。
有人可以帮忙吗?
【问题讨论】:
-
你解决了吗?我有同样的问题。谢谢克里斯。
标签: python html css django templates