【问题标题】:Using Twitter Bootstrap radio buttons with Flask在 Flask 中使用 Twitter Bootstrap 单选按钮
【发布时间】:2013-04-04 20:09:59
【问题描述】:

我正在构建一个基于 Web 的仪表板,我想使用来自 Twitter Bootstrap 的单选按钮来帮助创建查询,然后针对 MongoDB(通过 Flask)运行,然后刷新 具有新填充数据的相同页面。我是构建基于 Web 的仪表板的新手,如果有更好的方法,请告诉我。

{% 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?

【问题讨论】:

    标签: python twitter-bootstrap flask jinja2


    【解决方案1】:

    如果您有大量类似的控件 - 为它们使用循环的最佳方式。假设我们使用 list 来存储所有按钮名称,以及其他活跃的按钮列表。这会给我们一些控制器:

    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表达式来简化,应该是这样的:

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

    但我现在没有 Jinja2,而且语法可能有误。

    如果我没有正确回答您的问题 - 请写一些评论,我会尝试更新我的答案:)

    【讨论】:

      【解决方案2】:

      至于设置活动框,我不确定你的意思,但是按钮标签属性autofocus 可能会有所帮助。最初将这些字段留空可能是个好主意。

      您可以使用以下 Flask 代码在不使用 JavaScript 的情况下将选定的框传递给 Flask(您的 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")
      

      【讨论】:

        猜你喜欢
        • 2017-10-04
        • 2013-02-28
        • 1970-01-01
        • 2012-09-29
        • 2013-08-08
        • 2012-10-18
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多