實現搜索功能

  1. 準備視圖函數search()
    @app.route('/search/')
    def search():
       pass
  2. 修改base.html 中搜索輸入框所在的
    1. <form action="{{ url_for('search') }}" method="get">
    2.    <input name="q" type="text" placeholder="請輸入關鍵字">
<form action="{{ url_for('search') }}" method="get">
    <img src="{{ url_for('static',filename='image/5.jpg') }}"style="height: 40px;width: 45px; ">
    <a href="http://www.iqiyi.com/">愛奇藝官網</a>
    <a href="http://www.aiqiyibofangqi.com/">APP下載</a>
    <a href="{{ url_for("wenda") }}">用戶問答</a>
    <a href="{{ url_for("shouye") }}">首頁</a>
    <input name="q" type="text" placeholder="請輸入搜索內容">
    <input type="submit" value="搜索">
</form>
  1. 完成視圖函數search()
    1. 獲取搜索關鍵字
      q = request.args.get('q’)
    2. 條件查詢
      qu = Question.query.filter(Question.title.contains(q)).order_by('-creat_time’)
    3. 加載查詢結果:
      return render_template('index.html', question=qu)

  2. 組合條件查詢
    from sqlalchemy import or_, and_ 

示例:html

Lobby.query.filter(
    or_(
        and_(
            Lobby.id == Team.lobby_id,
            LobbyPlayer.team_id == Team.id,
            LobbyPlayer.player_id == player.steamid
        ),
         and_(
            Lobby.id == spectator_table.c.lobby_id,
            spectator_table.c.player_id == player.steamid
        )
    )
)sql

@app.route('/search/')
def search():
    qu=request.args.get('q')
    ques=Question.query.filter(
        or_(
            Question.title.contains(qu),
            Question.detail.contains(qu),
        )
    )
    return render_template('shouye.html',questions=ques)

 

https://staapp

相關文章
相關標籤/搜索