在我的 Django 應用程式中,在一個批處理中,有多個查詢。每個查詢都有一個相關的候選人。一個候選人有多個地址。我想生成一個包含所有候選人及其相關地址的 PDF。我正在使用 Wea??syprint 生成 PDF。
模型.py
class Batch(models.Model):
batch = models.CharField(verbose_name='Reference Number', max_length=200)
class Candidate(models.Model):
name = models.CharField(help_text='', max_length=200)
nationality = models.CharField(max_length=200)
class Address(models.Model):
DEFAULT_COUNTRY_ID = 1
PERA= 'Permanant Address'
PREA= 'Present Address'
ADDRESS_TYPE_CHOICES = [
(PERA, 'Permanant Address'),
(PREA, 'Present Address'),
]
candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE, verbose_name='Candidate')
nid = models.CharField(verbose_name='NID', max_length = 15, unique = True, null=True, blank=False)
address_type = models.CharField(max_length=200, choices=ADDRESS_TYPE_CHOICES)
address_line_1 = models.CharField('Address Line 1', max_length=1000, null=True, blank=True)
state = models.ForeignKey(State, on_delete=models.CASCADE,)
city = models.ForeignKey(City, on_delete=models.CASCADE,)
country = models.ForeignKey(Country, on_delete=models.CASCADE, default=DEFAULT_COUNTRY_ID)
class Inquiry(models.Model):
batch = models.ForeignKey(Batch, on_delete=models.CASCADE, null=True,)
candidate = models.ForeignKey(Candidate, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(verbose_name='Name', help_text='', max_length=200)
nid = models.CharField(verbose_name='NID', max_length = 15, null=True, blank=False)
這是我嘗試過的。但它只會生成第一個候選者的多個實體。如何生成包含所有候選人及其相關地址的 PDF?
視圖.py
def download_batch_report(request, pk):
inquiries = Inquiry.objects.filter(batch=pk)
batch = get_object_or_404(Batch, id=pk)
for inquiry in inquiries:
inquiry = get_object_or_404(Inquiry, id=inquiry.id)
candidate = get_object_or_404(Candidate, nid=inquiry.nid)
addresses = Address.objects.filter(candidate=candidate)
print_time = timezone.now()
font_config = FontConfiguration()
context = {'batch': batch, 'inquiries': inquiries, 'inquiry': inquiry, 'candidate': candidate,'addresses': addresses, 'print_time': print_time}
html_string = render_to_string('ssvr/batch/report.html', context).encode(encoding="UTF-8")
css = CSS(string='''
@font-face {
font-family: kalpurush;
src: url(file:///mnt/d/djangoprojects/dj-boot-modal/librarydb/static/librarydb/fonts/kalpurush.ttf);
}''', font_config=font_config)
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename=' str(batch.batch) '_' str(timezone.now()) '.pdf'
HTML(string=html_string).write_pdf(response, stylesheets=[css], presentational_hints=True, font_config=font_config)
return response
報告.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
{% load tz %}
<head>
<meta charset="UTF-16">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{ batch.batch }}</title>
</head>
<body>
{% for inquiry in inquiries %}
<div id="content" >
<div >
<div >
1. Full Name: {{ candidate.name }} <br>
2. Nationality: {{ candidate.nationality }} <br>
<hr>
</div>
<div >
{% for address in addresses %}
{% if address.address_type == 'Permanant Address' %}
3. Permanant Address: {{ address.address_line_1 }}, {{ address.city }}, {{ address.state }}, {{ address.country }}
{% endif %}
{% endfor %} <br>
{% for address in addresses %}
{% if address.address_type == 'Present Address' %}
4. Present Address: {{ address.address_line_1 }}, {{ address.city }}, {{ address.state }}, {{ address.country }}
{% endif %}
{% endfor %}
</div> <br>
<hr>
<br>
<div>
<div>
<p>
Report Generated at:
{% localtime on %}
{{ print_time|timezone:"Asia/Dhaka" }}
{% endlocaltime %}
</p>
</div>
</div>
<div ></div>
{% endfor %}
</body>
</html>
uj5u.com熱心網友回復:
無需Candidate在背景關系中傳遞物件。用于inquiry.candidate訪問附加到每個查詢的候選人。地址也一樣。用于inquiry.candidate.address_set.all回圈遍歷這些。
此外,您的視圖中不需要 for 回圈,因為您在報告中執行它。無論如何,在回圈的第一次運行時,代碼將回傳并退出函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437664.html
標籤:for循环 django-views django-模板 django-weasyprint
上一篇:r中多列的rowsum
下一篇:使用for回圈在R中利用spotifyrget_artist_audio_features函式,跳過回圈中的錯誤
