Django在easy-pdf中的sqlite内容

时间:2017-07-06 09:17:20

标签: python django sqlite pdf

Django版本:1.11.3

我已将数据存储在sqlite数据库中,因为我希望以pdf格式显示。我用easy-pdf。我不知道如何在不使用render()的情况下解析数据。我如何使用get_context_data()来完成它。有什么建议吗?

这有效:

def test(request):
all_organizations = Organization.objects.all()
all_tickets = Ticket.objects.all()
context = {'all_organizations': all_organizations, 'all_tickets': all_tickets}
return render(request, 'test/docs/examples/theme/test.html', context)

不知道如何解析all_organizations和all_tickets以便在easy-pdf中使用:

class HelloPDFView(PDFTemplateView):
all_organizations = Organization.objects.all()
all_tickets = Ticket.objects.all()
context = {'all_organizations': all_organizations, 'all_tickets': all_tickets}
template_name = "test/docs/examples/theme/hello.html"
def get_context_data(self, **kwargs):
    return super(HelloPDFView, self).get_context_data(
        pagesize="A4",
        title="Test",
        **kwargs
    )

1 个答案:

答案 0 :(得分:0)

def get_context_data(self, **kwargs):
    context = super(HelloPDFView, self).get_context_data(**kwargs)
    context['all_organizations'] = Organization.objects.all() 
    context['all_tickets'] = Ticket.objects.all()
    context['pagesize']="A4"
    context['title']= 'Test'
    return context

试试这个,在模板中你可以使用关键名称来访问数据,例如pagesize,title和所有