我的django模板中有一个html页面,它是pdf类型的,for loop
应该根据object model returns
的数量生成transactions
,但我希望每个transaction
都在一个完整的html页面中,而另一个transaction
是net页面,我试过这样做,但似乎有点困难,因为我是django html的新手。
{% extends "base.html" %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{% block title %} Report {% endblock title %}
<div id="container">
{% block content %}
{% for transaction in transactions %}
{% if transaction.complete %}
<table class="tg">
<thead>
<tr>
<th class="tg-fv77" colspan="12" rowspan="2"><span style="color:#3531FF">Report For Tenant ( {{ transaction.chp_reference }} )</span></th>
</tr>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky" colspan="9">CHP Reference</td>
<td class="tg-0pky" colspan="3">{{transaction.chp_reference}}</td>
</tr>
<tr>
<td class="tg-0pky" colspan="9">Rent Effective From (dd/mm/yyyy)</td>
<td class="tg-0pky" colspan="3">{{transaction.rent_effective_date}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">CRA Fortnightly Rates valid for 6 months from</td>
<td class="tg-0lax" colspan="3">{{transaction.cra_rate_from}}</td><hr>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Market Rent of the Property</td>
<td class="tg-0lax" colspan="3">{{transaction.property_market_rent}}</td>
</tr>
<tr>
<td class="tg-0lax" colspan="9">Number of Family Group(s)</td>
<td class="tg-0lax" colspan="3">{{transaction.number_of_family_group}}</td>
</tr>
</tbody>
</table>
{% if transaction.complete %}
<table class="tg" style="undefined;table-layout: fixed; width: 714px">
<colgroup>
<col style="width: 86px">
<col style="width: 201px">
<col style="width: 109px">
<col style="width: 63px">
<col style="width: 121px">
<col style="width: 134px">
</colgroup>
<thead>
<tr>
{% for f in transaction.family_groups.all %}
<th style="text-align:left">Family No</th>
<th style="text-align:left">Income Type</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Rent %</th>
<th style="text-align:left">Weekly Income</th>
<th style="text-align:left"><span style="background-color:#ffe0c7">Rent Component</span></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-4erg">{{f.name}}</td>
<td >{% for m in f.family_members.all %}{{ m.num_of_family_members }}<br>{% endfor %}</td>
<td >{% for m in f.family_members.all %} {{m.name}} <br> {% endfor %}</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.effective_rent_percentage}} <br> {% endfor %}
</td>
<td class="tg-4erg">{% for m in f.family_members.all %} {{m.income}} <br> {% endfor %}</td>
<td class="tg-1qbe">{% for m in f.family_members.all %} {{m.income_component|stringformat:".2f"}} <br> {% endfor %}</td>
</tr>
<tr>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky" colspan="9">CHP Reference</td>
<td class="tg-0pky" colspan="3">{{transaction.chp_reference}} Insufficient Info.</td>
<hr>
</tr>
</tbody>
</table>
{% endif %}
</table>
{% endfor %}
{% endblock content %}
</div>
</html>
您可以使用xhtml2pdf
将html文件转换为pdf文件。还有许多其他选择。
from django.template.loader import get_template,render_to_string
import xhtml2pdf.pisa as pisa
from io import BytesIO
def render_to_file(path: str, params: dict):
template = get_template(path)
html = template.render(params)
file_name = "report.pdf"
file_path = os.path.join(os.path.abspath(os.path.dirname("__file__")), "charts", file_name)
with open(file_path, 'wb') as pdf:
pisa.pisaDocument(BytesIO(html.encode("UTF-8")), pdf, link_callback=BASE_DIR + '/static/charts/Logo/')
return [file_name, file_path]
其中"path"是指向html模板的路径,params是contect字典(在您的情况下是事务数据(。
请阅读xhtml2pdf的文档,因为它不支持应用于普通html页面的所有css。