简体   繁体   中英

Flask render_template() returning “NameError: name 'app' is not defined”

I am following the Quickstart Guide for Flask. http://flask.pocoo.org/docs/quickstart/#static-files I'm getting this error as I follow the guide.

/application
/__init__.py
/templates
    /hello.html

   @app.route('/hello/')
   @app.route('/hello/<name>')
   def hello(name=None):
       return render_template('hello.html', name=name)

    >python _init_.py 
        Traceback (most recent call last):
        File "_init_.py", line 4, in <module>
   @app.route('/hello/')
   NameError: name 'app' is not defined

Perhaps this?

from flask import Flask, request, render_template

唯一缺少的是

from flask import Flask, render_template

Look at the minimal program on the Flask website , especially the first two lines:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

You problem is that you haven't imported a Flask module and haven't initialized a Flask app object .

I am surprised no one has marked this question answered. As many have suggested, I can also confirm that from flask import Flask, render_template solves the problem

You need to:

from flask import request

I think its a flaw in Flask's quickstart guide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM