使用Twitter Bootstrap样式的单选按钮与Flask应用程序配合使用

11

我正在构建一个基于Web的仪表板,希望使用Twitter Bootstrap提供的单选按钮来帮助创建查询,进而通过FlaskMongoDB进行交互,并刷新相同页面以显示新的数据。由于我是新手,如果有更好的方法,请告诉我。

{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="span4 offset4">
        <div class="well">
            <legend>Click me!</legend>
            <form method="POST" action="" accept-charset="UTF-8">
                {% if error %}
                    <div class="alert alert-error">
                        <a class="close" data-dismiss="alert" href="#">x</a>{{ error }}
                    </div>
                {% endif %}

                <div id="radios1" class="btn-group view-opt-btn-group" data-toggle="buttons-radio">
                    <button type="button" class="btn active" name="choice1" value="A">A</button>
                    <button type="button" class="btn" name="choice1" value="B">B</button>
                    <button type="button" class="btn" name="choice1" value="C">C</button>
                    <button type="button" class="btn" name="choice1" value="D">D</button>
                    <button type="button" class="btn" name="choice1" value="E">E</button>
                    <input type="hidden" name="choice1" value={{request.form['choice1']}} />
                </div>

                <script type="text/jscript">
                    $("body").find("#radios1").children().each(function () {
                        $(this).bind('click', function () {
                            $("input[name=choice1]").val(this.value);
                            });
                    });
                </script>

                <button class="btn-info btn" input type="submit">Submit</button>
            </form>
        </div>
    </div>
</div>
{% endblock %}

这个代码段生成了单选按钮,并使用JavaScript代码来检测哪个按钮被点击,然后通过request.Form对象将数据发送回进行处理。

如何在屏幕更新后设置每个按钮栏的活动框?我是否需要编写某种{{if request.option1 == ?}}块来为每个按钮定义class="btn active",还是有更聪明(或更明显)的方法?此外,如何设置默认值/初始化条件?我应该在请求对象中预填充相应字段吗?

另外,是否可能在不使用上面的JavaScript代码的情况下将所选框传递给Flask?

2个回答

11

如果您有一组相似的控件,最好使用循环来处理它们。假设我们使用列表来存储所有按钮名称,并使用另一个列表存储活动按钮。这将给我们带来一些控制器:

from flask import render_template

@app.route('/form/')
def hello(name=None):
    return render_template('hello.html', buttons=['A', 'B', 'C'], active_btns=['A', 'C'])

因此,在模板中我们会有类似以下的内容:

<div id="radios1" class="btn-group view-opt-btn-group" data-toggle="buttons-radio">
{% for button in buttons %}
    {% if button in active_btns %}
        <button type="button" class="btn active" name="choice1" value="{{ button }}">{{ button }}</button>
    {% else %}
        <button type="button" class="btn" name="choice1" value="{{ button }}">{{ button }}</button>
    {% endif %}
{% endfor %}
</div>

实际上,for循环内部的表达式可以简化,使用Jinja的if表达式,应该是这样的:

事实上,使用Jinja的If表达式可以简化for循环中的表达式,并且应该像这样:

<button type="button" class="btn{{" active" if button in active_btns}}" name="choice1" value="{{ button }}">{{ button }}</button>

但我现在没有Jinja2,语法也可能有错误。

如果我没有理解你的问题,请留下评论,我会尽力更新我的回答 :)


9
关于设置活动框,我不确定您的意思,但是按钮标签属性autofocus可能会有所帮助。最好一开始就将字段留空。
您可以使用以下Flask代码将所选框传递给Flask而无需JavaScript(当单击提交按钮时,您的HTML当前已正确设置以将请求传递给Flask):
from flask import Flask, render_template, request

@app.route("/", methods = ["GET", "POST"])
def home():
    if request.method == "POST":

        button = request.form['choice1'] #this retrieves which radio button was pressed

        if button == 'A': #if the button with attribute value = "A' is pressed
            #what you want to do when button A is pressed
        elif button == 'B':
            #what you want to do when button B is pressed
        elif button == 'C':
            #what you want to do when button C is pressed
        elif button == 'D':
            #what you want to do when button D is pressed
        elif button == 'E':
            #what you want to do when button E is pressed
    return render_template("home.html")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接