Python 405在Django3中生成pdf时出错
我按照本教程生成PDF,效果很好 问题是,当我使用post请求生成pdf时,它会显示405错误。我正在使用post方法访问客户id以生成发票 这是我的GeneratePDF课程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('
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,我真傻