NameError: global name 'render_template' is not defined(关键词:flask/bug)

在Flask项目中遇到`NameError: global name 'render_template' is not defined`的错误。问题源于在hello.py文件中未导入`render_template`。为解决此问题,只需在代码中导入相应的模板渲染函数即可修复这个bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目结构:

/home/henry/dev/flask_henry
hello.py  hello.pyc  README.md  templates  venv_flask_henry

hello.py代码:

from flask import Flask
from flask_script import Manager

app = Flask(__name__)

manager = Manager(app)

@app.route('/')
def index():
    return render_template('index.html')

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

if __name__ == '__main__':
    manager.run()

报错:

(venv_flask_henry) henry@henry-virtual-machine:~/dev/flask_henry$ python hello.py runserver --host 0.0.0.0
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[2017-11-26 14:11:30,844] ERROR in app: Exception on /user/HeLi [GET]
Traceback (most recent call last):
  File "/home/henry/dev/flask_henry/venv_flask_henry/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/henry/dev/flask_henry/venv_flask_henry/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/henry/dev/flask_henry/venv_flask_henry/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/henry/dev/flask_henry/venv_flask_henry/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/henry/dev/flask_henry/venv_flask_henry/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "hello.py", line 14, in user
    return render_template('user.html', name=name)
NameError: global name 'render_template' is not defined
127.0.0.1 - - [26/Nov/2017 14:11:30] "GET /user/HeLi HTTP/1.1" 500 -

注意到这一行:
NameError: global name ‘render_template’ is not defined

原因是:
hello.py中没有导入render_template

补上

from flask import render_template

即可。

python代码为: from flask import Flask,request,render_template app = Flask(__name__) @app.route('/',methods = ['GET','POST']) def test(): if request.method == 'GET': return render_template('login.html') else: return 'I get POST!' if __name__ == '__main__': app.run(debug=True) html文件代码为: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Get与Post请求</title> </head> <body> <h2>如果收到Get请求,返回这个HTML页面</h2> <form method="post"> <button type="submit"> 按下我发送POST请求 </button> </form> </body> </html> 运行时出现以上错误,是什么原因,要如何解决: E:\202312100217-Ljw\flask_web\envs\python.exe "D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path E:\202312100217-Ljw\路由\get_post.py Testing started at 8:44 ... D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import iter_entry_points Launching pytest with arguments E:\202312100217-Ljw\路由\get_post.py --no-header --no-summary -q in E:\202312100217-Ljw\路由 ============================= test session starts ============================= collecting ... collected 1 item get_post.py::test FAILED [100%] get_post.py:3 (test) @app.route('/',methods = ['GET','POST']) def test(): > if request.method == 'GET': get_post.py:6: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ..\flask_web\envs\lib\site-packages\werkzeug\local.py:432: in __get__ obj = instance._get_current_object() ..\flask_web\envs\lib\site-packages\werkzeug\local.py:554: in _get_current_object return self.__local() # type: ignore _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ name = 'request' def _lookup_req_object(name): top = _request
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值