HTML 表单(python、flask):有一个按钮向方法提交唯一的参数

我现在有一个很好的 HTML 表单,可以通过按钮调用方法。

我想要做的是创建几个调用相同方法的新按钮,但每个按钮传递不同的参数。

具体用例是:我的按钮现在使用 get_random_album() 检索随机相册。

我想制作“爵士”、“古典”和“摇滚”音乐分别调用 get_random_album(genre=<genre>). 的按钮

移动平均维数:

from flask import Flask,redirect

# Below import commented out for MVE

# from discogs import get_random_album


# TODO: look at more advanced form builders


# Create app instance

app = Flask(__name__)


# Create homepage with button

@app.route("/")

def home():

    return """

    <html><body>

    <h2> Spin yo records </h2>

        <form action="/query">

            <input type='submit' value="I'm Feeling Lucky">

    </body></html>

    """


# Backend query to discogs

@app.route("/query")

def link():

    # return redirect(get_random_album())

    # Real implementation commented out

    # Scratch implementation here

    def get_random_album(genre=None):

        url = "https://www.google.com" 

        if genre == 'Jazz':

            return url + "/search?q=jazz"

        if genre == 'Classical':

            return url + "/search?q=classical"

        if genre == 'Rock':

            return url + "/search?q=rock"

        return url


    return redirect(get_random_album())

# Run app

if __name__ == "__main__":

    app.run(debug=True,port=600)

实际项目


智慧大石
浏览 118回答 1
1回答

胡子哥哥

首先创建具有相同name但不同的按钮value@app.route("/")def home():&nbsp; &nbsp; return """&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h2> Spin yo records </h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Jazz">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Classic">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Rock">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="I'm Feeling Lucky">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>&nbsp; &nbsp; """接下来,您可以使用 和按钮中使用的名称来选择valuerequestfrom flask import requestimport random@app.route("/query")def link():&nbsp; &nbsp; allowed_values = ('Jazz', 'Classic', 'Rock')&nbsp; &nbsp; genre = request.args.get("selected_genre")&nbsp; &nbsp; # "I'm Feeling Lucky"&nbsp; &nbsp; if genre not in allowed_values:&nbsp; &nbsp; &nbsp; &nbsp; genre = random.choice(allowed_values)&nbsp; &nbsp; genre = genre.lower()&nbsp; &nbsp; url = f"https://www.google.com/search?q={genre}"&nbsp;&nbsp; &nbsp; return redirect(url)完整示例from flask import Flask, redirect, requestimport randomapp = Flask(__name__)@app.route("/")def home():&nbsp; &nbsp; return """&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h2> Spin yo records </h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Jazz">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Classic">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Rock">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="I'm Feeling Lucky">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>&nbsp; &nbsp; """@app.route("/query")def link():&nbsp; &nbsp; allowed_values = ('Jazz', 'Classic', 'Rock')&nbsp; &nbsp; genre = request.args.get("selected_genre")&nbsp; &nbsp; if genre not in allowed_values:&nbsp; &nbsp; &nbsp; &nbsp; genre = random.choice(allowed_values)&nbsp; &nbsp; genre = genre.lower()&nbsp; &nbsp; url = f"https://www.google.com/search?q={genre}"&nbsp;&nbsp; &nbsp; return redirect(url)if __name__ == "__main__":&nbsp; &nbsp; app.run(debug=True,port=600)在以前的版本中,它在 url 中发送&nbsp;value&nbsp;,即。&nbsp;/query?selected_genre=Rock&nbsp;- 这样每个人都可以看到它,或者很容易它可以尝试使用自己的价值。这就是为什么我使用&nbsp;allowed_values&nbsp;来阻止它。要从 url 中隐藏选定的流派,您必须使用:<form ... method="POST">@app.route(..., methods=['GET', 'POST'])(或methods=['POST'])request.form代替request.args完整示例from flask import Flask, redirect, requestimport randomapp = Flask(__name__)@app.route("/")def home():&nbsp; &nbsp; return """&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h2> Spin yo records </h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Jazz">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Classic">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="Rock">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="selected_genre" value="I'm Feeling Lucky">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>&nbsp; &nbsp; """@app.route("/query", methods=['GET', 'POST'])def link():&nbsp; &nbsp; allowed_values = ('Jazz', 'Classic', 'Rock')&nbsp; &nbsp; genre = request.form.get("selected_genre")&nbsp; &nbsp; if genre not in allowed_values:&nbsp; &nbsp; &nbsp; &nbsp; genre = random.choice(allowed_values)&nbsp; &nbsp; genre = genre.lower()&nbsp; &nbsp; url = f"https://www.google.com/search?q={genre}"&nbsp;&nbsp; &nbsp; return redirect(url)if __name__ == "__main__":&nbsp; &nbsp; app.run(debug=True, port=600)如果您想在按钮上使用不同的文本但仍发送相同的value,那么您可能需要隐藏 <input> 和 value 但是每个按钮都需要分开 <form>@app.route("/")def home():&nbsp; &nbsp; return """&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h2> Spin yo records </h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" value="Jazz" name="selected_genre">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="The Best Jazz Music">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" value="Classic" name="selected_genre">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="The Best Classic Music">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" value="Rock" name="selected_genre">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="The Best Rock Music">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" value="random" name="selected_genre">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="I'm Feeling Lucky">&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>&nbsp; &nbsp; """或者您必须使用 <button> 而不是 <input>@app.route("/")def home():&nbsp; &nbsp; return """&nbsp; &nbsp; <html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <h2> Spin yo records </h2>&nbsp; &nbsp; &nbsp; &nbsp; <form action="/query" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" name="selected_genre" value="Jazz">The Best Jazz Music</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" name="selected_genre" value="Classic">The Best Classic Music</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" name="selected_genre" value="Rock">The Best Rock Music</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" name="selected_genre" value="random">I'm Feeling Lucky</button>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>&nbsp; &nbsp; """
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5