首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在水瓶中,我如何重定向到被点击的配置文件?

问在水瓶中,我如何重定向到被点击的配置文件?
EN

Stack Overflow用户
提问于 2018-06-27 04:06:08
回答 2查看 808关注 0票数 2

我对酒瓶很陌生。我有一个mysql数据库和一个模板。假设我有3张照片

代码语言:javascript
复制
 <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中有以下代码:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
 <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>

​

然后,我用这些来生成一个新的模板:

代码语言:javascript
复制
 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)

希望有人能给我一个新的开始。提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-27 06:48:18

您可以通过路由url传递信息,而不是通过ajax传递信息。

experts.html

代码语言:javascript
复制
<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>

烧瓶路线

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2018-06-27 05:56:50

代码语言:javascript
复制
{% for profile in profiles %}
<a href="/profile?the_profile={{ profile.id }}"><img src="{{profile.img}}"></a>
{% endfor %}

...I猜的?也许吧?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51054397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档