首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Xhtml2PDF TypeError

Django Xhtml2PDF TypeError
EN

Stack Overflow用户
提问于 2020-12-31 19:25:52
回答 1查看 47关注 0票数 0

我正在尝试从Django的HTML文件中生成并强制下载PDF。以下是我采取的步骤和我执行的代码:

views.py:

代码语言:javascript
复制
class GeneratePDF(GenericView):
    def get(self, request, *args, **kwargs):
        template = get_template('dashboard/generated.html')
        context = {
            "invoice_id": 123,
            "customer_name": "John Cooper",
            "amount": 1399.99,
            "today": "Today",
        }
        html = template.render(context)
        pdf = render_to_pdf('invoice.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "Invoice_%s.pdf" %("12341231")
            content = "inline; filename='%s'" %(filename)
            download = request.GET.get("download")
            if download:
                content = "attachment; filename='%s'" %(filename)
            response['Content-Disposition'] = content
            return response
        return HttpResponse("Not found")

utils.py:

代码语言:javascript
复制
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

这里的问题是,当我运行视图时,我得到了这个错误:

代码语言:javascript
复制
TypeError at /pdf/

__init__() takes 1 positional argument but 2 were given

下面是完整的回溯:

代码语言:javascript
复制
Environment:


Request Method: GET
Request URL: http://localhost:8000/pdf/

Django Version: 2.2.3
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'dashboard.apps.DashboardConfig',
 'crispy_forms',
 'debug_toolbar',
 'wkhtmltopdf',
 'easy_pdf']
Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /pdf/
Exception Value: __init__() takes 1 positional argument but 2 were given

我所要做的就是使用上下文字典创建和下载PDF。接受1个参数而不是2个参数的函数是什么?我试着更改每一行,但总是得到相同的错误。utils.py中的函数有问题吗?

我从CodingForEntrepreneurs拿到了代码

编辑:添加urls.py

urls.py

代码语言:javascript
复制
path('pdf/', views.GeneratePDF, name='pdf'),
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-31 19:56:08

对于基于类的视图,在传递给url路径时需要调用<view>.as_view()

代码语言:javascript
复制
path('pdf/', views.GeneratePDF.as_view(), name='pdf'),
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65520240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档