视图.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response,render
from test.models import Order,Product
import preppy
from z3c.rml import rml2pdf
testToGeneratePDF(request):
testList = ['123456','23456','456789']
#fetch the data about testList
orderLists = Order.objects.filter(order_id__in=(testList))
for value in orderLists:
#process orderList data and append a List element to each value
value.productList = Product.objects.filter(product_id = value.product_id)
# Load the rml template into the preprocessor ...
template = preppy.getModule(settings.BASE_DIR + '/PDFKit/A4.prep')
BASE_DIR = settings.BASE_DIR
# ... and do the preprocessing.
rmlText = template.get(datetime.datetime.now().strftime("%Y-%m-%d"), BASE_DIR, orderLists)
# Then generate the *.pdf output ...
pdf = rml2pdf.parseString(rmlText)
# Finally return response to browser
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename; filename="orders.pdf"'
response.write(pdf.read())
return response
现在,我的问题是如何在模板中循环 productList 的数据。
Preppy 不处理 RML 模板中的语法如下:
{{for row in orderLists}}
{{ for value in row.data }}
{{endfor}}
{{endfor}}
如何解决迭代器的问题?
非常感谢大家