1

我正在使用 Django-CMS,但是当我尝试在 views.py 中使用 xhtml2pdf 示例时,出现此错误:

TemplateSyntaxError 您必须启用“sekizai.context_processors.sekizai”模板上下文处理器或使用“sekizai.context.SekizaiContext”来呈现您的模板。

如果我在没有 Sekizai 的 Django 项目中使用该示例没问题,有什么建议吗?

谢谢。

设置.py

THIRD_PARTY_APPS = (
    ...
    'sekizai',
    ...
)

base.html

{% load cms_tags menu_tags sekizai_tags static i18n %}
...

应用程序/htmltopdf_appviews.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa

# Create your views here.
def render_pdf_view(request):
    template_path = 'htmltopdf_app/cv_to_pdf.html'
    context = {'myvar': 'this is your template context'}

    # Create a Django response object, and specify content_type as pdf
    response = HttpResponse(
        content_type='application/pdf'
    )

    response['Content-Disposition'] = 'attachment; filename="report.pdf"'

    # find the template and render it.
    template = get_template(template_path)
    html = template.render(context)

    # create a pdf
    pisa_status = pisa.CreatePDF(
        html,
        dest=response
    )

    # if error then show some funy view
    if pisa_status.err:
        return HttpResponse('We had some errors <pre>' + html + '</pre>')
    return response
4

0 回答 0