Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取模板名称(self)不';无法在TemplateView中工作_Python_Django_Django Templates_Django Views_Django Class Based Views - Fatal编程技术网

Python 获取模板名称(self)不';无法在TemplateView中工作

Python 获取模板名称(self)不';无法在TemplateView中工作,python,django,django-templates,django-views,django-class-based-views,Python,Django,Django Templates,Django Views,Django Class Based Views,我正在尝试使用django-pdfkit中的PDFView。问题是我没有一个模板。相反,我有4个模板。选择的模板取决于request.GET参数 因此,我创建了一个名为Test的视图,它扩展了PDFView class Test(PDFView): def get_template_names(self): # don't bother with chosing template for now return ["pdf/dobropis_pdf_tem

我正在尝试使用
django-pdfkit
中的
PDFView
。问题是我没有一个模板。相反,我有4个模板。选择的模板取决于request.GET参数

因此,我创建了一个名为Test的
视图,它扩展了
PDFView

class Test(PDFView):

    def get_template_names(self):
        # don't bother with chosing template for now
        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}
问题是,如果我没有指定
模板\u名称
,它将返回:

TemplateDoesNotExist位于/render/doklad/None

因此我假设
get\u template\u names
方法甚至没有被调用

为什么会这样?我怎样才能让它工作

这来自
url.py

url(r'^render/doklad/', views.Test.as_view(), name="doklad_to_pdf"),
这是一个
PDFView
(如果有帮助):


通过覆盖
get
,您已经绕过了该视图的所有内置功能,包括调用
get\u template\u name
。这很少是正确的做法


但是,考虑到您所展示的代码,您为什么不能自己从
render\uhtml
调用该方法?或者直接在那里内联逻辑。

通过覆盖
get
,您已经绕过了该视图的所有内置功能,包括调用
get\u模板\u名称。这很少是正确的做法


但是,考虑到您所展示的代码,您为什么不能自己从
render\uhtml
调用该方法?或者直接在那里内联逻辑。

将模板名称作为参数附加到URL中?这有什么帮助?我可以通过检查请求来选择模板。“获取模板名称”不起作用。请将模板名称作为参数附加到URL中?这有什么帮助?我可以通过检查请求来选择模板。有一个问题是获取模板名称不起作用。我几乎只使用基于函数的视图,所以我不想扩展PDFView。最后,我得到了它的工作。Thanks我几乎只使用基于函数的视图,所以我不想扩展PDFView。最后,我得到了它的工作。谢谢
class PDFView(TemplateView):
    #: Set to change the filename of the PDF.
    filename = None

    #: Set to default the PDF display to inline.
    inline = False

    #: Set pdfkit options dict.
    pdfkit_options = None

    def get(self, request, *args, **kwargs):
        """
        Return a HTTPResponse either of a 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) and '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 the PDF and returns as bytes.

        :rtype: bytes
        """
        html = self.render_html(*args, **kwargs)

        options = self.get_pdfkit_options()
        if 'debug' in self.request.GET and 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 a default dict of options to supply to pdfkit.

        :rtype: dict
        """
        if self.pdfkit_options is not None:
            return self.pdfkit_options

        return {
            'page-size': 'A4',
            'encoding': 'UTF-8',
        }

    def get_filename(self):
        """
        Return ``self.filename`` if set otherwise return the template basename with a ``.pdf`` extension.

        :rtype: str
        """
        if self.filename is None:
            name = splitext(basename(self.template_name))[0]
            return '{}.pdf'.format(name)

        return self.filename

    def render_html(self, *args, **kwargs):
        """
        Renders the 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)

        with 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