我对酒瓶很陌生。我有一个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代码片段是用expert.html编写的。我几乎没有列出上面列出的SQL查询,render_template中的route1()中的第二个参数的数据是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)希望有人能给我一个新的开始。提前感谢!
发布于 2018-06-27 06:48:18
您可以通过路由url传递信息,而不是通过ajax传递信息。
experts.html
<a href="{{ url_for('route2') }}/profile1"><div><img src="pathOfImage"></div></a>
<a href="{{ url_for('route2') }}/profile2"><div><img src="pathOfImage"></div></a>
<a href="{{ url_for('route2') }}/profile3"><div><img src="pathOfImage"></div></a>烧瓶路线
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('/profile/<id>')
def route2(id):
# id variable contains user profile id string as per user click
# depending on id set variables client_info and skill_info and render
return render_template("profile.html", client_info=client_info, skill_info=skill_info)发布于 2018-06-27 05:56:50
{% for profile in profiles %}
<a href="/profile?the_profile={{ profile.id }}"><img src="{{profile.img}}"></a>
{% endfor %}...I猜的?也许吧?
https://stackoverflow.com/questions/51054397
复制相似问题