0

I am developing an app similar to FB. I am trying to render a friends profile. When I click on the link that should render the profile page I get a Bad Request Error

I am using flask, python, html and JS

Here's the python code on the server side:

@app.route('/friends_Profile', methods=["GET"])
def friends_profile():
    userid = (request.args['x'],)
    print(userid)
    print(type(userid))

    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    c.execute("SELECT Name FROM users WHERE rowid = ?", userid)
    info = c.fetchall()

    print(info[0][0])



    return render_template('friends-profile.html', friend=info)



Here's the html:


  <div class='friends'>
            <b>You are Friends with:</b>
            {% for x in range(friends|length) %}
            <li><a id='{{friends[loop.index-1][0][0]}}'
             href='/friends_Profile' name='{{friends[loop.index-1][0][1]}}_{{friends[loop.index-1][0][2]}}'>{{friends[loop.index -1][0][1]}} {{friends[loop.index -1][0][2]}}</a></li>

            {% endfor %}

      </div>
        </div>

And here is the JavaScript:

$(document).ready(function(){
  $("a").click(function(){

    let x = $(this).attr('id');
    console.log(x);
    $.ajax("/friends_Profile?x="+x)

  });
});

And here is the Friends Profile page:

{% extends "layout.html" %}

{% block title %}

    Friend

{% endblock %}

{% block main %}

    <h1>This is {{friend}}'s profile</h1>

{% endblock %}

Why am I getting a Bad Request? I know the problem is with request.args['x'] but can't figure out how to fix it...

Fabian Omobono
  • 81
  • 2
  • 13

1 Answers1

0

In the python code you do friend = info

But on the html you do friends|length

To fix the error change the python code to friends = info

Kavan Doctor
  • 187
  • 5