使用烧瓶的错误请求

时间:2019-12-17 18:34:23

标签: javascript python html flask

我正在开发与FB类似的应用。 我正在尝试呈现朋友个人资料。 当我单击应显示个人资料页面的链接时,出现错误请求错误

我正在使用flask,python,html和JS

这是服务器端的python代码:

@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)



这是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>

这是JavaScript:

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

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

  });
});

这是“朋友个人资料”页面:

{% extends "layout.html" %}

{% block title %}

    Friend

{% endblock %}

{% block main %}

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

{% endblock %}

为什么我收到一个错误的请求? 我知道问题出在request.args ['x']上,但是不知道如何解决...

1 个答案:

答案 0 :(得分:0)

在python代码中,您进行friend = info

但是在html上您可以friends|length

要解决该错误,请将python代码更改为friends = info