UnboundLocalError:局部变量';项目';赋值前引用(python、flask)

UnboundLocalError:局部变量';项目';赋值前引用(python、flask),python,flask,runtime-error,Python,Flask,Runtime Error,注意:不是重复的,因为类似的堆栈溢出问题没有帮助我理解此问题的解决方案。谢谢大家! 如何修复此错误?我不明白,因为我认为我做了与另一种方法完全相同的模式,这不会造成问题。我看了其他关于这个问题的答案,想不出来。请注意,我是如何毫无问题地传入的,但是当我尝试传入checkin=item时,它会抛出此错误。(在return语句中,两者都位于各自函数的末尾。) 错误: UnboundLocalError:分配前引用了局部变量“item”。行:[会话['user\u id',会话['user\u id'

注意:不是重复的,因为类似的堆栈溢出问题没有帮助我理解此问题的解决方案。谢谢大家!

如何修复此错误?我不明白,因为我认为我做了与另一种方法完全相同的模式,这不会造成问题。我看了其他关于这个问题的答案,想不出来。请注意,我是如何毫无问题地传入的,但是当我尝试传入
checkin=item
时,它会抛出此错误。(在return语句中,两者都位于各自函数的末尾。)

错误: UnboundLocalError:分配前引用了局部变量“item”。行:
[会话['user\u id',会话['user\u id',每页]),签入=项)

我的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)
@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)
{% 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>&mdash; {{ message.pub_date|datetimeformat }}</small>
  {% else %}
    <li><em>There's no message so far.</em>
  {% endfor %}
  </ul>
{% endblock %}
我的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)
@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)
{% 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>&mdash; {{ message.pub_date|datetimeformat }}</small>
  {% else %}
    <li><em>There's no message so far.</em>
  {% endfor %}
  </ul>
{% endblock %}
timeline.html(烧瓶模板):

@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)
@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)
{% 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>&mdash; {{ message.pub_date|datetimeformat }}</small>
  {% else %}
    <li><em>There's no message so far.</em>
  {% endfor %}
  </ul>
{% endblock %}
{%extends“layout.html”%}
{%block title%}
{%if request.endpoint=='public\u timeline%}
公共时间线
{%elif request.endpoint=='user\u timeline%}
{{profile_user.username}的时间线
{%else%}
我的时间表
{%endif%}
{%endblock%}
{%block body%}
{{self.title()}}
{%if g.user%}
{%if request.endpoint=='user\u timeline%}
{%if g.user.user\u id==profile\u user.user\u id%}
这就是你!
{%elif后跟%}
您当前正在跟踪此用户。
.
{%else%}
您尚未跟踪此用户。
.
{%endif%}
{%elif request.endpoint=='时间线'%}
你在想什么{{g.user.username}?

{%endif%}
{%endif%}
    {%if签入%} 最近签入:
  • 地点:{{checkin['vention']['name']}}
    地址:{{checkin['vention']['location']['Address']}}
    呼喊:{{checkin['Shout']}}
    {%elif g.user%} 此用户没有签入。 {%endif%}
    其他信息: {消息%中的消息为%s}
  • {{message.text} &mdash;{{message.pub_date|datetimeformat} {%else%}
  • 目前还没有消息。 {%endfor%}
{%endblock%}
结果是我在“item”行前面有一个制表符,在“return”行前面有四个空格,所以它在不同的范围内处理它。答案是用1个制表符替换“return”行前面的4个空格,然后错误就消失了

item = dict['response']['checkins']['items'][0]
return render_template('timeline.html',messages=query_db('''...