Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 下载填充了Django表单的HTML模板页面_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python 下载填充了Django表单的HTML模板页面

Python 下载填充了Django表单的HTML模板页面,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我有一个模板页面,当我填写我的Django表单时,我可以在上面访问该页面。此页面是一个简单的HTML页面,数据来自我的表单。我想能够下载填充模板。也就是说,获得一个浏览器窗口,允许下载模板,或者单击一个按钮将HTML模板保存在某个位置(因为此HTML模板需要在邮件中插入) 这是我的视图.py文件中的代码: class TemplateGenerator(TemplateView): ''' This class displays the form and lets to save dat

我有一个模板页面,当我填写我的
Django表单时,我可以在上面访问该页面。此页面是一个简单的
HTML页面
,数据来自我的表单。我想能够下载填充模板。也就是说,获得一个浏览器窗口,允许下载模板,或者单击一个按钮将HTML模板保存在某个位置(因为此HTML模板需要在邮件中插入)

这是我的视图.py文件中的代码:

class TemplateGenerator(TemplateView):
    ''' This class displays the form and lets to save data into my database.
        It redirects users to the filled HTML template '''
    form_class = CommunicationForm
    template_name = 'form.html'
    success_url = '/HTML_Result/'

    def get(self, request):
        form = self.form_class()
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = self.form_class(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('HTML_Result')
        args = {'form': form}
        return render(request, self.template_name, args)


class HTMLResult(TemplateView):
    ''' This class displays the template filled thanks to the form from TemplateGenerator() class '''
    template_name = 'template_1.html'

    def get(self, request):
        data = Communication.objects.values().latest('id')
        self.donwload_html()
        return render(request, self.template_name, {'data': data})

    def donwload_html(self):
        file_path = os.path.join(settings.MEDIA_ROOT, 'media')
        if os.path.exists(file_path):
            with open(file_path, 'rb') as fh:
                response = HttpResponse(fh.read(),
                                        content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                response['Content-Disposition'] = 'inline; filename=' + 'test'
                return response
        raise Http404
当我尝试访问媒体文件夹时,
file\u path
拒绝了我的权限。 但在我看来,与我想要得到的相比,我的函数可能是错误的

你知道吗

我知道我需要使用
Content-Disposition
来下载HTML页面,但我没有找到正确使用它的方法


通过advance谢谢您

您可以创建一个视图,使用该模板和数据创建一个填充的html模板以发送电子邮件,而不是下载填充的模板@谢谢你的回答。第一次,我不需要创建电子邮件发送功能。它应该在第二阶段。我的问题是:如何下载已填充的模板?我不相信Django中真的存在该功能。它的业务不是创建东西并保存它们,而是创建东西并发送它们(给web浏览器、api用户等)。但是,您可以使用django
render_to_string
函数,然后将字符串保存到文件中。