python - Django / xhtml2pdf - object has no attribute 'encode' -


i'm having hard time producing pdf's in django. per previous question i'm running same error.
when run following code 'list' object has no attribute 'encode', pdf saved in media folder plain text file. think object being referred 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 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 django http response     file.seek(0)     pdf = file.read()     file.close()            # don't forget close 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 error:

environment:   request method: 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 @ /lld/tesco-greenfield-datacenter-deployment/pdf/ exception value: 'list' object has no attribute 'encode' 

i can't believe i've spent few days crying , tearing hair out because using google font. yep, once removed <link href="http://fonts.googleapis.com/css?family=lato:400,700" rel="stylesheet" type="text/css"> template, error vamoosed!


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -