[英]how to encode dynamic django variable from a html template in python coding
我想将“{{variable}}”和“{%for loop ...%}”从模板编码为名为“html”的变量。 这样我就可以使用pisa.CreatePDF将其渲染为PDF文件。 我使用xhtml2pdf生成PDF。
html="<html><body>{% for inventory in filter.qs %} {{ inventory.pk }} {{ inventory.name }} {{ inventory.price }} {{ inventory.quantity }} {% endfor %} </body></html>".
上面的代码不起作用。
它没有从数据库中提供任何值。 只需以下文字:PDF输出中的{%for inventory.qs%} {{inventory.pk}} {{inventory.name}} {%endfor%}。
像这样的东西:
from django.template import Template, Context
html = """
<html>
<body>
{% for inventory in inventories %}
{{ inventory.pk }} {{ inventory.name }} {{ inventory.price }} {{ inventory.quantity }}
{% endfor %}
</body>
</html>
"""
t = Template(html)
inventories = Inventory.objects.all()
c = Context({'filter': filter})
raw_html = t.render(c)
print(raw_html)
有关详细信息,请访问: https : //docs.djangoproject.com/en/2.2/ref/templates/api/#rendering-a-context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.