2

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

Now, My question is that how to loop the data of productList in the template.

Preppy doesn't process the syntax in RML template as follow:

{{for row in orderLists}}
    {{ for value in row.data }} 
    {{endfor}}
{{endfor}}

how to solve the iterator's problem?

Thanks a lot for everyone

Macfee Joe
  • 21
  • 2

0 Answers0