Django/Djangoeasypdf:“NoneType”对象没有属性“encode”

2024-09-21 02:43:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使django-easy-pdf工作,但收到以下错误:

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 render_lld_pdf
  79.                                   {'document': document, 'sections': sections})
File "/usr/local/lib/python2.7/dist-packages/easy_pdf/rendering.py" in render_to_pdf_response
  166.         pdf = render_to_pdf(template, context, encoding=encoding, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/easy_pdf/rendering.py" in render_to_pdf
  139.     return html_to_pdf(content, encoding, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/easy_pdf/rendering.py" in html_to_pdf
  65.                             link_callback=link_callback, **kwargs)
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
  372.         cssFile = self.c.getFile(cssResourceName, relative=self.rootPath)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in getFile
  822.             return getFile(self._getFileDeprecated(name, relative))
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: 'NoneType' object has no attribute 'encode'

根据这些:

'NoneType' object has no attribute 'encode'

'NoneType' object has no attribute 'encode' error in django

我已检查我的模型是否返回unicode对象:

^{pr2}$

这是我的观点:

def render_lld_pdf(request, slug):
    document = get_object_or_404(Document, slug=slug)
    sections = \
    get_list_or_404(Section.objects.filter
                    (associated_document__startswith=document.slug))
    return render_to_pdf_response(request, 'lld/print_lld.html',
                              {'document': document, 'sections': sections},
                              encoding=u'utf-8')

我不知道怎么解决这个问题?在

编辑:在print documentprint type(document)中添加可以得到:

Greenfield Datacenter Deployment
<class 'lld.models.Document'>
No handlers could be found for logger "xhtml2pdf"

Tags: djangoinpyselfpdflibpackagesusr
2条回答

我甚至不敢相信我已经哭了好几天了,因为我用的是谷歌字体。是的,一旦我从模板中删除了<link href="http://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css">,错误就消失了!在

在挖掘xhml2pdf之后,我看到@RMPhoenix是正确的,唯一的解决方案(目前)是删除引起此错误的CSS。回溯清楚地表明CSS解析器引发了一个错误。在

但更重要的是我认为这段代码:

xhtml2pdf/parser.py in pisaParser at line 711:

    if default_css:
        context.addDefaultCSS(default_css)
    pisaPreLoop(document, context)
    #try:
    context.parseCSS()
    #except:
    #    context.cssText = DEFAULT_CSS
    #    context.parseCSS()
    # context.debug(9, pprint.pformat(context.css))
    pisaLoop(document, context)

相关问题 更多 >

    热门问题