【问题标题】:Flask Web App: Filter table view by buttonFlask Web App:按按钮过滤表视图
【发布时间】:2019-06-04 15:30:40
【问题描述】:

我希望能够通过按钮(即域)过滤此 html 页面中的表格。在这里查看Html View。例如,如果有人点击摄影师,只有摄影师会出现。

烧瓶功能:

@app.route('/find_artist',methods=['POST', 'GET'])
def find_artist():
    if session.get("email"):
        if request.method == 'GET':
            return render_template('find_artist.html')
        curl = mysql.connection.cursor()
        curl.execute("SELECT * FROM freelancers")
        data = curl.fetchall()
        curl.close()
        return render_template(
            "find_artist.html",
            freelancers=data)
    else:
        return redirect(url_for('login'))

按钮和表格的 HTML 代码:

<div id="myBtnContainer">
  <button class="btn active" > Show all</button>
  <button class="btn" > Illustrator</button>
  <button class="btn"> Animator</button>
  <button class="btn" > Digital Designer</button>
  <button class="btn">Photographer</button>
  <button class="btn"> Filmmaker</button>
  <button class="btn"> Graphic Design</button>
  </div>

    <thead>
        <tr>
          <th scope="col">Serial</th>
          <th scope="col">First Name </th>
          <th scope="col">Last Name</th>
          <th scope="col">Email</th>
          <th scope="col">Domain</th>
          <th scope="col">Experience</th>
        </tr>
      </thead>
      <tbody id="myTable">
        {% for item in freelancers %}
        <tr>
          <td>{{item["id"]}}</td>
          <td>{{item["first_name"]}}</td>
          <td>{{item["last_name"]}}</td>
          <td>{{item["email"]}}</td>
          <td>{{item["domain"]}}</td>
          <td>{{item["experience"]}}</td>
        </tr>
        {% endfor %}

谢谢你:)

【问题讨论】:

    标签: python html mysql flask


    【解决方案1】:

    对我有用的解决方案是为每个按钮创建一个单独的端点。

    def multiple_buttons(condition):
        if session.get("email"):
            curl = mysql.connection.cursor()
            curl.execute("SELECT * FROM freelancers WHERE domain LIKE %s", [condition])
            data = curl.fetchall()
            curl.close()
            return render_template(
                "find_artist.html",
                freelancers=data)
        else:
            return redirect(url_for('login'))
    
    @app.route('/showall',methods=['POST'])
    def showall():
        return multiple_buttons(None)
    
    
    @app.route('/illustrator',methods=['POST'])
    def illustrator():
        return multiple_buttons('Illustrator')
    

    HTML:

    <form action="/showall" method="post" style="display:inline-block">
          <button class="btn active" name="showall"  type="submit"> Show All</button>
      </form>
      <form action="/illustrator" method="post" style="display:inline-block">
          <button class="btn" name="illustrator"  type="submit"> Illustrator</button>
      </form>
    

    这个答案很有帮助:https://stackoverflow.com/a/42859886/8131701

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 2021-10-06
      • 2014-08-23
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多