[英]Django Xhtml2PDF TypeError
I am trying to generate and force download a PDF from an HTML file in Django.我正在尝试从 Django 中的 HTML 文件生成并强制下载 PDF。 Here are the steps I took and the codes I did:以下是我采取的步骤和我所做的代码:
views.py:视图.py:
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:实用程序.py:
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
The issue here is when I run the view, I get this error:这里的问题是当我运行视图时,我收到了这个错误:
TypeError at /pdf/
__init__() takes 1 positional argument but 2 were given
Here is the full Traceback:这是完整的追溯:
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
All I want to do is to create and download a PDF using a context dictionnary.我要做的就是使用上下文字典创建和下载 PDF 。 What is the function that takes 1 argument not two?需要 1 个参数而不是 2 个参数的 function 是什么? I tried changing in every line but I always get the same error.我尝试在每一行中进行更改,但总是遇到相同的错误。 Is there something wrong with the function in the utils.py? utils.py 中的 function 有问题吗?
I took the code from CodingForEntrepreneurs我从 CodingForEntrepreneurs 拿了代码
Edit: added urls.py编辑:添加 urls.py
urls.py网址.py
path('pdf/', views.GeneratePDF, name='pdf'),
For class based views you need to call <view>.as_view()
when passing to a url path对于基于 class 的视图,您需要在传递到 url 路径时调用<view>.as_view()
path('pdf/', views.GeneratePDF.as_view(), name='pdf'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.