UnboundLocalError: local variable 'item' referenced before assignment (python, flask)
UnboundLocalError: local variable 'item' referenced before assignment (python, flask)
注意:不是重复的因为类似的堆栈溢出问题并没有帮助我理解这个问题的解决方案。谢谢!
如何修复此错误?我不明白,因为我认为我所做的模式与另一种不会引起问题的方法完全相同。我查看了对此的其他 Whosebug 答案,但无法弄清楚。请注意我是如何毫无问题地传递 listOfCheckins=list
的,但是当我尝试传递 checkin=item
时,它会抛出此错误。 (两者都在各自函数的最后,在 return 语句中。)
错误:
UnboundLocalError:赋值前引用了局部变量 'item'。行:[session['user_id'], session['user_id'], PER_PAGE]),checkin=item)
我的 python flask 服务器的片段 有问题:
@app.route('/')
def timeline():
"""Shows a users timeline or if no user is logged in it will
redirect to the public timeline. This timeline shows the user's
messages as well as all the messages of followed users.
"""
if not g.user:
return redirect(url_for('public_timeline'))
#Get info from foursquare
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''
select message.*, user.* from message, user
where message.author_id = user.user_id and (
user.user_id = ? or
user.user_id in (select whom_id from follower
where who_id = ?))
order by message.pub_date desc limit ?''',
[session['user_id'], session['user_id'], PER_PAGE]),checkin=item)
我的 python flask 服务器的片段没有引起任何问题:
@app.route('/foursquare')
def foursquare():
"""Shows your foursquare info. Or, if you have not authorized this app to connect to
foursquare, then it will redirect you to foursquare.
"""
if not g.user:
return redirect(url_for('public_timeline'))
result = query_db('select access_token_text from access_token where user_id = ?',
[session['user_id']], one=True)
if not result:
return redirect("https://foursquare.com/oauth2/authenticate?response_type=code&client_id=" + FOURSQUARE_CLIENT_ID + "&redirect_uri=" + FOURSQUARE_REDIRECT_URI,code=302)
else:
#Get info from foursquare
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
list = dict['response']['checkins']['items']
return render_template('foursquare.html', listOfCheckins=list)
timeline.html(烧瓶模板):
{% extends "layout.html" %}
{% block title %}
{% if request.endpoint == 'public_timeline' %}
Public Timeline
{% elif request.endpoint == 'user_timeline' %}
{{ profile_user.username }}'s Timeline
{% else %}
My Timeline
{% endif %}
{% endblock %}
{% block body %}
<h2>{{ self.title() }}</h2>
{% if g.user %}
{% if request.endpoint == 'user_timeline' %}
<div class=followstatus>
{% if g.user.user_id == profile_user.user_id %}
This is you!
{% elif followed %}
You are currently following this user.
<a class=unfollow href="{{ url_for('unfollow_user', username=profile_user.username)
}}">Unfollow user</a>.
{% else %}
You are not yet following this user.
<a class=follow href="{{ url_for('follow_user', username=profile_user.username)
}}">Follow user</a>.
{% endif %}
</div>
{% elif request.endpoint == 'timeline' %}
<div class=twitbox>
<h3>What's on your mind {{ g.user.username }}?</h3>
<form action="{{ url_for('add_message') }}" method=post>
<p><input type=text name=text size=60><!--
--><input type=submit value="Share">
</form>
</div>
{% endif %}
{% endif %}
<ul class=messages>
{% if checkin %}
Most recent checkin:
<li><strong>Venue:</strong> {{ checkin['venue']['name'] }}<br>
<strong>Address:</strong> {{ checkin['venue']['location']['address'] }}<br>
<strong>Shout:</strong> {{ checkin['shout'] }} <br>
{% elif g.user %}
This user has no checkins.
{% endif %}
<br>Other Messages:
{% for message in messages %}
<li><img src="{{ message.email|gravatar(size=48) }}"><p>
<strong><a href="{{ url_for('user_timeline', username=message.username)
}}">{{ message.username }}</a></strong>
{{ message.text }}
<small>— {{ message.pub_date|datetimeformat }}</small>
{% else %}
<li><em>There's no message so far.</em>
{% endfor %}
</ul>
{% endblock %}
原来我在 "item" 行前面有 1 个制表符,在 "return" 行前面有 4 个空格,所以它在不同的范围内处理它。答案是用 1 个制表符替换 "return" 行前面的 4 个空格,然后错误就消失了。
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''...
注意:不是重复的因为类似的堆栈溢出问题并没有帮助我理解这个问题的解决方案。谢谢!
如何修复此错误?我不明白,因为我认为我所做的模式与另一种不会引起问题的方法完全相同。我查看了对此的其他 Whosebug 答案,但无法弄清楚。请注意我是如何毫无问题地传递 listOfCheckins=list
的,但是当我尝试传递 checkin=item
时,它会抛出此错误。 (两者都在各自函数的最后,在 return 语句中。)
错误:
UnboundLocalError:赋值前引用了局部变量 'item'。行:[session['user_id'], session['user_id'], PER_PAGE]),checkin=item)
我的 python flask 服务器的片段 有问题:
@app.route('/')
def timeline():
"""Shows a users timeline or if no user is logged in it will
redirect to the public timeline. This timeline shows the user's
messages as well as all the messages of followed users.
"""
if not g.user:
return redirect(url_for('public_timeline'))
#Get info from foursquare
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''
select message.*, user.* from message, user
where message.author_id = user.user_id and (
user.user_id = ? or
user.user_id in (select whom_id from follower
where who_id = ?))
order by message.pub_date desc limit ?''',
[session['user_id'], session['user_id'], PER_PAGE]),checkin=item)
我的 python flask 服务器的片段没有引起任何问题:
@app.route('/foursquare')
def foursquare():
"""Shows your foursquare info. Or, if you have not authorized this app to connect to
foursquare, then it will redirect you to foursquare.
"""
if not g.user:
return redirect(url_for('public_timeline'))
result = query_db('select access_token_text from access_token where user_id = ?',
[session['user_id']], one=True)
if not result:
return redirect("https://foursquare.com/oauth2/authenticate?response_type=code&client_id=" + FOURSQUARE_CLIENT_ID + "&redirect_uri=" + FOURSQUARE_REDIRECT_URI,code=302)
else:
#Get info from foursquare
token = result['access_token_text']
response = requests.get(FOURSQUARE_API_BASE + "users/self/checkins?oauth_token=" + token +
"&v=20150326&m=foursquare")
dict = json.loads(response.text)
list = dict['response']['checkins']['items']
return render_template('foursquare.html', listOfCheckins=list)
timeline.html(烧瓶模板):
{% extends "layout.html" %}
{% block title %}
{% if request.endpoint == 'public_timeline' %}
Public Timeline
{% elif request.endpoint == 'user_timeline' %}
{{ profile_user.username }}'s Timeline
{% else %}
My Timeline
{% endif %}
{% endblock %}
{% block body %}
<h2>{{ self.title() }}</h2>
{% if g.user %}
{% if request.endpoint == 'user_timeline' %}
<div class=followstatus>
{% if g.user.user_id == profile_user.user_id %}
This is you!
{% elif followed %}
You are currently following this user.
<a class=unfollow href="{{ url_for('unfollow_user', username=profile_user.username)
}}">Unfollow user</a>.
{% else %}
You are not yet following this user.
<a class=follow href="{{ url_for('follow_user', username=profile_user.username)
}}">Follow user</a>.
{% endif %}
</div>
{% elif request.endpoint == 'timeline' %}
<div class=twitbox>
<h3>What's on your mind {{ g.user.username }}?</h3>
<form action="{{ url_for('add_message') }}" method=post>
<p><input type=text name=text size=60><!--
--><input type=submit value="Share">
</form>
</div>
{% endif %}
{% endif %}
<ul class=messages>
{% if checkin %}
Most recent checkin:
<li><strong>Venue:</strong> {{ checkin['venue']['name'] }}<br>
<strong>Address:</strong> {{ checkin['venue']['location']['address'] }}<br>
<strong>Shout:</strong> {{ checkin['shout'] }} <br>
{% elif g.user %}
This user has no checkins.
{% endif %}
<br>Other Messages:
{% for message in messages %}
<li><img src="{{ message.email|gravatar(size=48) }}"><p>
<strong><a href="{{ url_for('user_timeline', username=message.username)
}}">{{ message.username }}</a></strong>
{{ message.text }}
<small>— {{ message.pub_date|datetimeformat }}</small>
{% else %}
<li><em>There's no message so far.</em>
{% endfor %}
</ul>
{% endblock %}
原来我在 "item" 行前面有 1 个制表符,在 "return" 行前面有 4 个空格,所以它在不同的范围内处理它。答案是用 1 个制表符替换 "return" 行前面的 4 个空格,然后错误就消失了。
item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''...