【发布时间】:2018-06-27 04:06:08
【问题描述】:
我对 Flask 很陌生。我有一个 mysql 数据库和一个模板。假设我有 3 张图片
<a href="#"><div><img src= pathOfImage id="profile1"/></div></a>
<a href="#"><div><img src= pathOfImage id="profile2"/></div></a>
<a href="#"><div><img src= pathOfImage id="profile3"/></div></a>
每张图片的id(profile1,profile2,profile3)是数据库中一些表的主键。我想要做的是通过使用主键找到该元组的相应属性的值。然后,将这些值从元组加载到模板中。
而且,我在 Python 中有以下代码:
from flask import *
@app.route("/")
def index():
return render_template("index.html")
@app.route('/experts')
def route1():
return render_template("experts.html", data=data)
我上面给出的 HTML 代码的 sn-p 在expert.html 中。我几乎没有上面列出的SQL查询,route1()中render_template中第二个参数的数据是SQL元组,它生成所有这些图像和ID。
我尝试在图像旁边放置一个按钮,并为该按钮提供 id。然后,使用 Ajax 将 id 作为变量传递给 python 脚本,并获取 SQL 元组。
但是,这并不是最难的部分。困难的部分是制作新路线并加载内容。我尝试使用“app.route”创建一条新路由并将数据传递给render_template的第二个参数。但是,它没有重定向到新的配置文件,并且在我点击配置文件之前调用了该方法。
之前,我使用按钮来检索 id:
<html>
<body>
<a href="{{ url_for('route2') }}"> <button id='1'>Button1</button></a>
<a href="{{ url_for('route2') }}"><button id='2'>Button2</button></a>
<a href="{{ url_for('route2') }}"><button id='3'>Button3</button></a>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"</script>
<script>
$(document).ready(function() {
$('button').click(function(event) {
var the_id = event.target.id;
$.ajax({
url: "/get_id",
type: "get",
data: {the_id: the_id},
success: function(response) {
},
error: function(xhr) {
}
});
})});
</script>
而且,我用这些来生成一个新模板:
import flask
from flask import *
from flaskext.mysql import MySQL
@app.route("/")
def index():
return render_template("index.html")
@app.route('/experts')
def route1():
return render_template("experts.html", data=data)
@app.route('/get_id')
@app.route('/profile')
def route2():
button_id '"' + flask.request.args.get('the_id') + '"'
//some code here to get the tuples that I need "client_info" and
//"skill_info" variable below
return render_template("profile.html", client_info=client_info,
skill_info=skill_info)
希望有人能重新开始。提前致谢!
【问题讨论】:
-
您应该向我们展示您尝试过的代码。
-
好的,一秒钟。我会更新的
-
@KlausD。刚刚更新
-
我完全不明白你的问题是什么......或者这与pycharm有什么关系......
-
@joran,请忽略我以前的内容。如果我有 3 张个人资料图片,然后单击其中一张,我希望它重定向到该个人资料