local variable 'request' referenced before assignment
<p>So, here's my ajax call to a specific route in my flask app:</p>

<pre><code>    $.ajax({
        url: '/addfriend',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(obj.user),
        success: function(results) {
            console.log(results);
            $('#u-' + obj.user.id + '').css('background-color', 'grey');
            $('#u-' + obj.user.id + '').val('Pending');
        }
    })

}
</code></pre>

<p>So in my python code, here's the function:</p>

<pre><code>@app.route('/addfriend', methods=['POST'])
def addFriend():
  if request.method == 'POST':

    data = json.loads(request.data)
    print data

    request = FriendRequests(sender_id=login_session['userid'],
                                recipient_id=data['id'])

    session.add(request)
    session.commit()

    response = make_response(json.dumps('Add Successful!'), 200)
    return response
</code></pre>

<p>but i get this error:</p>

<p>UnboundLocalError: local variable 'request' referenced before assignment</p>

<p>This is the only route doing this. Help?</p>