Django / xhtml2pdf - 对象没有属性'encode' [英] Django / xhtml2pdf - object has no attribute 'encode'

查看:218
本文介绍了Django / xhtml2pdf - 对象没有属性'encode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在Django中生成pdf。根据我的上一个问题我是遇到相同的错误。

当我运行以下代码时,我得到'list'对象没有属性'encode',pdf保存在媒体文件夹,但它是一个纯文本文件。
我认为被引用的对象是 queryset。

 code> @login_required 
def generate_pdf(request,slug):
#准备上下文
document = get_object_or_404(Document,slug = slug)
sections = \
get_list_or_404(Section.objects.filter
(associated_document__startswith = document.slug))
data = {'document':document,'sections':sections}

#渲染html内容通过html模板与上下文
template = get_template('lld / lld_pdf.html')
html = template.render(Context(data))
file = open('/ home /项目/ media / test.pdf',w + b)
打印类型(文档)
打印类型(部分)
打印类型(数据)
打印类型)
打印类型(html)
打印类型(文件)
pisaStatus = pisa.CreatePDF(html.encode('utf-8'),dest = file)
#返回PDF文件gh一个Django HTTP响应
file.seek(0)
pdf = file.read()
file.close()#不要忘记关闭文件句柄
return HttpResponse(pdf,mimetype ='application / pdf')

cli输出:

 < class'lld.models.Document'> 
< type'list'>
< type'dict'>
< class'django.template.backends.django.Template'>
< class'django.utils.safestring.SafeText'>
< type'file'>

和错误:

 环境:


请求方法:GET
请求URL:http:// localhost:8001 / lld / tesco-greenfield-datacenter-deployment / pdf /

Django版本:1.8.2
Python版本:2.7.6
安装的应用程序:
('django_admin_bootstrapped',
'django.contrib .admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages ',
'django.contrib.staticfiles',
'lld',
'registration')
安装的中间件:
('django.contrib.sessions.middleware .SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.M essageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


追溯:
文件/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.pyin get_response
132. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.pyin _wrapped_view
22. return view_func(request,* args,** kwargs)
文件/home/vagrant/shared/RepeatableDesign/lld/views.pyin generate_pdf
162. pisaStatus = pisa.CreatePDF(html.encode('utf-8'),dest = file)
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf/document.py在pisaDocument
89. encoding,context = context,xml_output = xml_output)
文件/ usr / local / lib / python2.7 / dist-packages / xhtml2pdf / document.pyin pisaStory
57. pisaParser(src,c ontext,default_css,xhtml,encoding,xml_output)
pisaParser中的/usr/local/lib/python2.7/dist-packages/xhtml2pdf/parser.py文件
685. context.parseCSS()
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py在parseCSS
498. self.css = self.cssParser.parse(self.cssText)
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py在解析
434. src,stylesheet = self._parseStylesheet(src)
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py在_parseStylesheet
522. src,stylesheetImports = self._parseAtImports(src)
文件/ usr / local / lib / python2.7 / dist-packages / xhtml2pdf / w3c / cssParser.pyin _parseAtImports
606. stylesheet = self.cssBuilder.atImport(import_,mediums,self)
文件 inImport
874中的/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/css.pyreturn cssParser.parseExternal(import_)
文件/ usr / loca l / lib / python2.7 / dist-packages / xhtml2pdf / context.pyin parseExternal
380. result = self.parse(cssFile.getData())
文件/ usr / local / lib / python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py在解析
434. src,stylesheet = self._parseStylesheet(src)
文件/ usr / local / lib / python2 7 / dist-packages / xhtml2pdf / w3c / cssParser.pyin _parseStylesheet
533. src,atResults = self._parseAtKeyword(src)
文件/usr/local/lib/python2.7/dist -packages / xhtml2pdf / w3c / cssParser.pyin _parseAtKeyword
655. src,result = self._parseAtFontFace(src)
文件/usr/local/lib/python2.7/dist-packages/ xhtml2pdf / w3c / cssParser.py在_parseAtFontFace
785. result = [self.cssBuilder.atFontFace(properties)]
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf /context.pyatFontFace
173. src = self.c.getFile(data [src],relative = self.c.cssParser.rootPath)
文件/ usr / local / lib /python2.7/d ist-packages / xhtml2pdf / context.pyin getFile
823. return getFile(name,relative或self.pathDirectory)
文件/usr/local/lib/python2.7/dist-packages/ getFile
635中的xhtml2pdf / util.py文件= pisaFileObject(* a,** kw)
文件/usr/local/lib/python2.7/dist-packages/xhtml2pdf/util。 pyin __init__
516. uri = uri.encode('utf-8')

异常类型:/ lld / tesco-greenfield-datacenter-deployment / pdf / $ b中的AttributeError $ b异常值:'list'对象没有属性'encode'


解决方案

我甚至不相信我花了几天时间哭泣着撕裂我的头发,因为我使用的是Google的字体。是的,一旦我删除了< link href =http://fonts.googleapis.com/css?family=Lato:400,700 =stylesheettype =text / css> 从我的模板,错误vamoosed!


I'm having a hard time producing pdf's in Django. As per my previous question I'm running into the same error.
When I run the following code I get 'list' object has no attribute 'encode', a pdf is saved in the media folder but it is a plain text file. I think the object that is being referred to is the sections queryset.

@login_required
def generate_pdf(request, slug):
    # Prepare context
    document = get_object_or_404(Document, slug=slug)
    sections = \
        get_list_or_404(Section.objects.filter
                        (associated_document__startswith=document.slug))
    data = {'document': document, 'sections': sections}

    # Render html content through html template with context
    template = get_template('lld/lld_pdf.html')
    html = template.render(Context(data))
    file = open('/home/project/media/test.pdf', "w+b")
    print type(document)
    print type(sections)
    print type(data)
    print type(template)
    print type(html)
    print type(file)
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file)
    # Return PDF document through a Django HTTP response
    file.seek(0)
    pdf = file.read()
    file.close()            # Don't forget to close the file handle
    return HttpResponse(pdf, mimetype='application/pdf')

cli output:

<class 'lld.models.Document'>
<type 'list'>
<type 'dict'>
<class 'django.template.backends.django.Template'>
<class 'django.utils.safestring.SafeText'>
<type 'file'>

and the error:

Environment:


Request Method: GET
Request URL: http://localhost:8001/lld/tesco-greenfield-datacenter-deployment/pdf/

Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
('django_admin_bootstrapped',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'lld',
 'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/vagrant/shared/RepeatableDesign/lld/views.py" in generate_pdf
  162.     pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/document.py" in pisaDocument
  89.                         encoding, context=context, xml_output=xml_output)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/document.py" in pisaStory
  57.     pisaParser(src, context, default_css, xhtml, encoding, xml_output)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/parser.py" in pisaParser
  685.     context.parseCSS()
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in parseCSS
  498.         self.css = self.cssParser.parse(self.cssText)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in parse
  434.                 src, stylesheet = self._parseStylesheet(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseStylesheet
  522.         src, stylesheetImports = self._parseAtImports(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtImports
  606.             stylesheet = self.cssBuilder.atImport(import_, mediums, self)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/css.py" in atImport
  874.             return cssParser.parseExternal(import_)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in parseExternal
  380.         result = self.parse(cssFile.getData())
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in parse
  434.                 src, stylesheet = self._parseStylesheet(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseStylesheet
  533.                 src, atResults = self._parseAtKeyword(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtKeyword
  655.             src, result = self._parseAtFontFace(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtFontFace
  785.         result = [self.cssBuilder.atFontFace(properties)]
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in atFontFace
  173.         src = self.c.getFile(data["src"], relative=self.c.cssParser.rootPath)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in getFile
  823.         return getFile(name, relative or self.pathDirectory)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/util.py" in getFile
  635.     file = pisaFileObject(*a, **kw)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/util.py" in __init__
  516.         uri = uri.encode('utf-8')

Exception Type: AttributeError at /lld/tesco-greenfield-datacenter-deployment/pdf/
Exception Value: 'list' object has no attribute 'encode'

解决方案

I can't even believe I've spent a few days crying and tearing my hair out because I was using a Google font. Yep, once I removed <link href="http://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css"> from my template, the error vamoosed!

这篇关于Django / xhtml2pdf - 对象没有属性'encode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆