gpt4 book ai didi

python - 为什么我得到这个 NameError : name 'url_for' is not defined?

转载 作者:太空狗 更新时间:2023-10-29 20:40:31 24 4
gpt4 key购买 nike

<分区>

我正尝试按照 flask 教程的说明构建 URL

http://flask.pocoo.org/docs/0.11/quickstart/

但是我一直收到这个 NameError

name 'url_for' is not defined

这是代码:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def Index() : pass

@app.route ('/ login')
def loGin() : pass

@app.route('/user/<username>')
def profile( username) : pass


with app.test_request_context() :
print url_for('Index')
print url_for('loGin')
print url_for('loGin', next='/')
print url_for('profile', username = 'Jon DD')

这是完整的错误信息:

eloiim:minimalapp iivri.andre$ export FLASK_APP=Greet.py
eloiim:minimalapp iivri.andre$ flask run
Traceback (most recent call last):
File "/usr/local/bin/flask", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 478, in main
cli.main(args=args, prog_name=name)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 345, in main
return AppGroup.main(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 388, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 124, in __init__
self._load_unlocked()
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 148, in _load_unlocked
self._app = rv = self.loader()
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 209, in load_app
rv = locate_app(self.app_import_path)
File "/usr/local/lib/python2.7/site-packages/flask/cli.py", line 89, in locate_app
__import__(module)
File "/Users/iivri.andre/Documents/minimalapp/Greet.py", line 16, in <module>
print url_for('Index')
NameError: name 'url_for' is not defined

我不确定是什么问题。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com