【发布时间】:2021-09-16 05:29:21
【问题描述】:
当我点击用户名时,它必须导航到包含特定用户信息的另一个页面。为了实现这一点,我尝试使用 jquery、python 和烧瓶。但我无法实现它,我收到以下错误。
谁能帮帮我。他们的代码有什么问题吗?
下面是代码
app.py
app.route("/ajaxfile",methods=["POST","GET"])
def ajaxfile():
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
if request.method == 'POST':
userid = request.form['userid']
cur.execute("SELECT * FROM user WHERE id = %s", [userid])
result = cur.fetchall()
return render_template('detail.html', result=result)
index.html
{% for row in emp %}
<tr>
<td>{{row.id}}</td>
<td><a data-id='{{row.id}}' class="userinfo btn btn-success" href="{{ url_for('ajaxfile') }}">{{row.name}}</a></td>
</tr>
<script type='text/javascript'>
$(document).ready(function(){
$('.userinfo').click(function(){
var userid= $(this).data('id');
alert(userid)
$.ajax({
url: '/ajaxfile',
type: 'post',
data: {userid: userid},
success: function(data){
}
});
});
});
</script>
details.html
{% for row in result %}
<table border='0' width='100%'>
<tr>
<td style="padding:20px;">
<p>Name : {{row.name}}</p>
<p>Description : {{row.description}}</p>
<p>Responsible person : {{row.responsibleperson}}</p>
<p>Employee code : {{row.employeename}}</p>
<p>Date : {{row.date_time}}</p>
</td>
</tr>
</table>
【问题讨论】:
-
您在
if范围内定义了结果变量,如果您发出发布请求,该变量将被执行。你在 render_template 函数中返回结果。如果没有发布请求,render_template 函数找不到任何变量result所以它给出错误 -
@charchit 可以为此提出解决方案。如果我删除一个 if 条件,我会得到 400 错误
标签: python html jquery mysql flask