python - get_template_names(self) doesn't work in TemplateView -
i'm trying use pdfview
django-pdfkit
. problem don't have 1 template. instead have 4 templates. template chosen depends on request.get parameter.
so i've created view
called test extends pdfview
.
class test(pdfview): def get_template_names(self): # don't bother chosing template return ["pdf/dobropis_pdf_template.html"] def get_context_data(self, **kwargs): doklad = get_object_or_404(doklad, pk=self.request.get.get('id')) return {'doklad':doklad}
the problem if don't specify template_name
, returns:
templatedoesnotexist @ /render/doklad/ none
so suppose get_template_names
method isn't called.
why so? how can make work?
this urls.py
:
url(r'^render/doklad/', views.test.as_view(), name="doklad_to_pdf"),
this pdfview
(in case helps):
class pdfview(templateview): #: set change filename of pdf. filename = none #: set default pdf display inline. inline = false #: set pdfkit options dict. pdfkit_options = none def get(self, request, *args, **kwargs): """ return httpresponse either of pdf file or html. :rtype: httpresponse """ if 'html' in request.get: # output html content = self.render_html(*args, **kwargs) return httpresponse(content) else: # output pdf content = self.render_pdf(*args, **kwargs) response = httpresponse(content, content_type='application/pdf') if (not self.inline or 'download' in request.get) , 'inline' not in request.get: response['content-disposition'] = 'attachment; filename=%s' % self.get_filename() response['content-length'] = len(content) return response def render_pdf(self, *args, **kwargs): """ render pdf , returns bytes. :rtype: bytes """ html = self.render_html(*args, **kwargs) options = self.get_pdfkit_options() if 'debug' in self.request.get , settings.debug: options['debug-javascript'] = 1 kwargs = {} wkhtmltopdf_bin = os.environ.get('wkhtmltopdf_bin') if wkhtmltopdf_bin: kwargs['configuration'] = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_bin) pdf = pdfkit.from_string(html, false, options, **kwargs) return pdf def get_pdfkit_options(self): """ returns ``self.pdfkit_options`` if set otherwise default dict of options supply pdfkit. :rtype: dict """ if self.pdfkit_options not none: return self.pdfkit_options return { 'page-size': 'a4', 'encoding': 'utf-8', } def get_filename(self): """ return ``self.filename`` if set otherwise return template basename ``.pdf`` extension. :rtype: str """ if self.filename none: name = splitext(basename(self.template_name))[0] return '{}.pdf'.format(name) return self.filename def render_html(self, *args, **kwargs): """ renders template. :rtype: str """ static_url = '%s://%s%s' % (self.request.scheme, self.request.get_host(), settings.static_url) media_url = '%s://%s%s' % (self.request.scheme, self.request.get_host(), settings.media_url) override_settings(static_url=static_url, media_url=media_url): template = loader.get_template(self.template_name) context = self.get_context_data(*args, **kwargs) html = template.render(context) return html
by overriding get
, you've bypassed all built-in functionality view, including calling of get_template_names
. right thing do.
however, given code you've shown, what's reason why can't call method render_html
? or inline logic there.
Comments
Post a Comment