【问题标题】:In Flask, how can I redirect to a profile being clicked?在 Flask 中,如何重定向到被点击的配置文件?
【发布时间】: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 张个人资料图片,然后单击其中一张,我希望它重定向到该个人资料

标签: python html flask


【解决方案1】:

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

专家.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)

【讨论】:

  • experts.html中每个div的ID不显示
  • 通过上面的代码访问experts.html,您应该会看到3 张图片,点击任何图片都会将您重定向到/profile/ url 并显示该特定配置文件。这不是你需要的吗?
【解决方案2】:
{% for profile in profiles %}
<a href="/profile?the_profile={{ profile.id }}"><img src="{{profile.img}}"></a>
{% endfor %}

...我猜?也许吧?

【讨论】:

  • 谢谢,我会尝试运行它并让您知道它是否有效
猜你喜欢
  • 2021-01-23
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2019-10-24
  • 2016-07-11
  • 1970-01-01
  • 2014-12-04
相关资源
最近更新 更多