AttributeError:“ NoneType”对象没有属性“ app”

下面的代码给出了错误:

Traceback (most recent call last):

File "pdf.py", line 14, in <module>

create_pdf(render_template('templates.htm'))

File "/usr/local/lib/python2.7/dist-packages/flask/templating.py", line 123, in render_template

ctx.app.update_template_context(context)

AttributeError: 'NoneType' object has no attribute 'app'

码:

from xhtml2pdf import pisa

from StringIO import StringIO

from flask import render_template,Flask

app=Flask(__name__)

app.debug=True

@app.route("/")

def create_pdf(pdf_data):

filename= "file.pdf"

pdf=pisa.CreatePDF( StringIO(pdf_data),file(filename, "wb"))

if __name__ == "__main__":

create_pdf(render_template('templates.htm'))

回答:

从代码中,我可以看到你希望允许用户下载pdf。

from xhtml2pdf import pisa

from StringIO import StringIO

from flask import render_template,Flask, Response

app=Flask(__name__)

app.debug=True

@app.route("/")

def create_pdf(pdf_data):

filename= "file.pdf"

pdf=pisa.CreatePDF( StringIO(pdf_data),file(filename, "wb"))

return Response(pdf, mimetype='application/octet-stream',

headers={"Content-Disposition": "attachment;filename=%s" % filename})

if __name__ == "__main__":

app.run()

现在开始 python aboveprogram.py

去 http://localhost:5000

以上是 AttributeError:“ NoneType”对象没有属性“ app” 的全部内容, 来源链接: utcz.com/qa/426586.html

回到顶部