Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 405在Django3中生成pdf时出错_Python_Django_Xhtml2pdf - Fatal编程技术网

Python 405在Django3中生成pdf时出错

Python 405在Django3中生成pdf时出错,python,django,xhtml2pdf,Python,Django,Xhtml2pdf,我按照本教程生成PDF,效果很好 问题是,当我使用post请求生成pdf时,它会显示405错误。我正在使用post方法访问客户id以生成发票 这是我的GeneratePDF课程 class GeneratePDF(View): def get(self, request, *args, **kwargs): if request.method == 'POST': template = get_template('

我按照本教程生成PDF,效果很好

问题是,当我使用post请求生成pdf时,它会显示405错误。我正在使用post方法访问客户id以生成发票

这是我的GeneratePDF课程

class GeneratePDF(View):
    def get(self, request, *args, **kwargs):
        
            if request.method == 'POST':
                template = get_template('head/invoice.html')
                context = {
                "customer":"aaaa"
                }
                html = template.render(context)
                pdf = render_to_pdf('head/invoice.html', context)
                if pdf:
                    response = HttpResponse(pdf, content_type='application/pdf')
                    filename = "Invoice_%s.pdf" %("12341231")
                    content = "inline; filename='%s'" %(filename)
                    download = request.GET.get("download")
                if download:
                    content = "attachment; filename='%s'" %(filename)
                response['Content-Disposition'] = content
                return response
            
            template = get_template('head/invoice.html')
            context = {
                "customer":"aaaa"
            }
            html = template.render(context)
            pdf = render_to_pdf('head/invoice.html', context)
            if pdf:
                response = HttpResponse(pdf, content_type='application/pdf')
                filename = "Invoice_%s.pdf" %("12341231")
                content = "inline; filename='%s'" %(filename)
                download = request.GET.get("download")
                if download:
                    content = "attachment; filename='%s'" %(filename)
                response['Content-Disposition'] = content
                return response
我没有编辑任何其他文件

这是来自服务器的响应

Method Not Allowed (POST): /employee/customer_printbill/
Method Not Allowed: /employee/customer_printbill/

我是django的初学者,我无法解决这个问题。请帮帮我。

您正在混合基于函数和基于类的视图。您应该在基于类的视图中定义
post
方法,请求将被分派到该
post
方法。因此,您不需要在
get
方法中检查
request.method
是否为
POST
,因为
POST
请求将由
POST
方法处理。有关更多信息,请参阅。

谢谢您让我知道这一点。在get方法上检查request.method,我真傻