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

2024-04-29 04:09:31 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码给出错误:

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'))

Tags: 代码infrompyimportapppdfcreate
3条回答

从代码中,我可以看到您希望允许用户下载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

浏览器提示下载PDF。 希望能帮上忙。。

Flask有很多“魔力”,所以您不必担心路由或解析请求。当Flask应用程序收到请求时,它会在将逻辑委托给视图函数之前创建一个“context”对象。

在代码中,您直接调用render_template而不经过Flask,因此不会创建上下文。render_template试图通过此上下文(ctx)到达应用程序(app),即None,因此错误:

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

这并不是你的代码唯一的问题。视图函数(在decorator@app.route(...)中注册)不能直接调用。@rajpy的回答给了你一个很好的例子,告诉你应该如何使用它们。

马丁的回答很好地解释了为什么会发生这个错误。

公认的答案解决了提出的问题,但肯定不是唯一的办法。在我的情况下,我有一些更像:

import threading

from flask import Flask, render_template

app = Flask("myapp")

app.route('/')
def get_thing(thing_id):
    thing = cache.get(thing_id)
    if thing is None:
        # Handle cache miss...
    elif is_old(thing):
        # We'll serve the stale content but let's
        # update the cache in a background thread
        t = threading.Thread(
            target=get_thing_from_datastore_render_and_cache_it,
            args=(thing_id,)
        )
        t.start()
    return thing

def get_thing_from_datastore_render_and_cache_it(thing_id):
    thing = datastore.get(thing_id)
    cache.set(render_template(thing))

但是当get_thing_from_datastore_render_and_cache_it在Flask请求周期之外的后台线程中运行时,我得到了上面显示的错误,因为该线程没有访问请求上下文的权限。

之所以出现此错误,是因为Flask提供了一个开发人员快捷方式,允许自动访问模板中的请求变量—换句话说,这是Flask决定如何包装Jinja2的功能而不是Jinja2本身造成的。我解决这个问题的方法就是直接使用Jinja2的渲染:

import jinja2

def render_without_request(template_name, **template_vars):
    """
    Usage is the same as flask.render_template:

    render_without_request('my_template.html', var1='foo', var2='bar')
    """
    env = jinja2.Environment(
        loader=jinja2.PackageLoader('name.ofmy.package','templates')
    )
    template = env.get_template(template_name)
    return template.render(**template_vars)

该函数假定Flask应用程序具有传统模板子文件夹。具体来说,这里的项目结构是

.
└── name/
    ├── ofmy/
    |   ├── package/
    |   |   ├── __init__.py <--- Where your Flask application object is defined
    |   |   └── templates/
    |   |       └── my_template.html
    |   └── __init__.py
    └── __init__.py

如果在templates/下有一个子目录结构,则只需从模板文件夹的根传递相对路径,就像使用Flask的render_template时一样。

相关问题 更多 >