如何让iterator使用z3c.rml在RML模板中生成pdf

时间:2015-03-10 07:21:59

标签: django rml listiterator

views.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}}

如何解决迭代器的问题?

非常感谢大家

0 个答案:

没有答案