在Flask中尝试使用url_for方法时出现错误。我不确定是什么原因,因为我只遵循烧瓶快速入门。我是一个有一点Python经验的Java人,想学习Flask。
下面是跟踪信息:
Traceback (most recent call last):
File "hello.py", line 36, in <module>
print url_for(login)
File "/home/cobi/Dev/env/flask/latest/flask/helpers.py", line 259, in url_for
if endpoint[:1] == '.':
TypeError: 'function' object has no attribute '__getitem__我的代码是这样的:
from flask import Flask, url_for
app = Flask(__name__)
app.debug = True
@app.route('/login/<username>')
def login(): pass
with app.test_request_context():
print url_for(login)我已经尝试过稳定版和开发版的Flask,但仍然出现错误。任何帮助都将不胜感激!谢谢你,如果我的英语不是很好,我很抱歉。
发布于 2012-10-14 13:15:52
docs说url_for接受字符串,而不是函数。您还需要提供用户名,因为您创建的路由需要用户名。
改为执行以下操作:
with app.test_request_context():
print url_for('login', username='testuser')您会收到此错误,因为字符串有__getitem__方法,而函数没有。
>>> def myfunc():
... pass
...
>>> myfunc.__getitem__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__getitem__'
>>> 'myfunc'.__getitem__
<method-wrapper '__getitem__' of str object at 0x10049fde0>
>>> https://stackoverflow.com/questions/12879490
复制相似问题