【问题标题】:How to display in Flask certain input fields depending on the value of variables如何根据变量的值在 Flask 中显示某些输入字段
【发布时间】:2022-02-08 18:27:19
【问题描述】:

祝你有美好的一天!

我将非常感谢并感谢在我考虑了很长时间的问题上提供帮助。

如何根据变量的值显示某些输入字段

Flask Bootstrap 的形式 输入组 多个输入 自定义表单

输入组包括对自定义选择和自定义文件输入的支持。不支持这些的浏览器默认版本。

我想为各种信息的条目显示一个表单。 表单,例如,由 20 个不同的输入元素组成,15-25 个不同的字段用于输入任何信息。

我有 25 个不同的变量,25 个变量可以取一个值,一个值 - 零 (0) 或一 (1)。

每个变量对应表单中的某个字段,用于输入任何信息。

而且我需要,例如,如果上述变量之一等于零 (0),那么我不会在表单中显示相应的字段以输入任何信息。

字段的数量可能会有所不同。 表单文本

<label for="inputPassword5" class="form-label">Password</label>
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
<div id="passwordHelpBlock" class="form-text">
  Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</div> 

【问题讨论】:

    标签: python flask


    【解决方案1】:

    这是一百万种方法。您需要澄清您的问题并显示您的代码。

    例如:

    • main.py
    from flask import Flask, render_template, request
    
    
    app = Flask(__name__)
    
    @app.route("/1")
    def hide_by_class():
    
        abc = {
            "a": str(request.args.get('a')),
            "b": str(request.args.get('b')),
            "c": str(request.args.get('c'))
        }
    
        return render_template('hide_by_class.html',  abc=abc)
    
    
    @app.route("/2")
    def hide_by_render():
    
        abc = {
            "a": str(request.args.get('a')),
            "b": str(request.args.get('b')),
            "c": str(request.args.get('c'))
        }
    
        return render_template('hide_by_render.html', abc=abc)
    
    
    if __name__ == "__main__":
        app.run()
    
    • templates/hide_by_render.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
      {% for k, v in abc.items() %}
        {% if v == "1" %}
            <label for="{{ k }}">{{ k }} is</label>
            <input type="text" id="{{ k }}" name="{{ k }}"><br>
        {% endif %}
      {% endfor %}
    
    </body>
    </html>
    
    • templates/hide_by_class.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="UTF-8">
       <title>Title</title>
       <style>
        .hidden {display: none;}
      </style>
    </head>
    <body>
    
      {% for k, v in abc.items() %}
        <label class="{{ 'hidden' if v !="1" else 'displayed' }}" for="{{ k }}">{{ k }} is</label>
        <input class="{{ 'hidden' if v !="1" else 'displayed' }}" type="text" id="{{ k }}" name="{{ k }}"><br>
      {% endfor %}
    
    </body>
    </html>
    

    如果您访问 URL http://127.0.0.1:5000/1?a=1&amp;b=0&amp;c=1

    所有abc 输入将出现在html 代码中,但只有ac 将由CSS 显示

    <html lang="en"><head>
       <meta charset="UTF-8">
       <title>Title</title>
       <style>
        .hidden {display: none;}
      </style>
    </head>
    <body>
    
      
        <label class="displayed" for="a">a is</label>
        <input class="displayed" type="text" id="a" name="a"><br>
      
        <label class="hidden" for="b">b is</label>
        <input class="hidden" type="text" id="b" name="b"><br>
      
        <label class="displayed" for="c">c is</label>
        <input class="displayed" type="text" id="c" name="c"><br>
      
    
    
    </body></html>

    如果您访问 URL http://127.0.0.1:5000/2?a=1&amp;b=0&amp;c=1

    b 根本不会出现在页面内

    <html lang="en"><head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        
            <label for="a">a is</label>
            <input type="text" id="a" name="a"><br>
        
            <label for="c">c is</label>
            <input type="text" id="c" name="c"><br>
    
    </body></html>

    【讨论】:

      【解决方案2】:

      对于与此类似的逻辑,您通常会使用基于规则的引擎。我发现了一个类似的话题:

      Python Rule Based Engine

      不依赖于诸如 Clips 之类的基于规则的引擎之一,它可能需要许多嵌套的 if 语句,这些语句不是很 Pythonic。

      很遗憾,如果没有使用其中任何一种解决方案,您可能希望开发一种可以充当规则引擎的服务。

      您也可以将每个输入存储为 0 或 1,并创建一个类似于位图的 25 个字符的字符串,并将每个结果与下面的字典进行比较:

      results_dict = {
         "1": "Field A",
         "2": "Field B"
      }
      
      def check_string(some_string):
          """
          :param some_string: a string of length 25 i.e. 0100000100000010000100001
          """
          count = 1
          show_variable_list = []
          for character in some_string:
              print(character)
              if character == "1":
                  print(True)
                  show_variable_list.append(results_dict.get(str(count)))
      
              count += 1
          return show_variable_list
      
      fields_to_show = check_string("1100")
      

      然后根据 fields_to_show 中存储的项目,您也许可以巧妙地处理要显示的内容!

      希望这会有所帮助,祝你好运!

      【讨论】:

      • 如何将模板表单中的选定字段传递到代码 Flask 中以进行进一步处理?页面上表单中设置的标记,模板html(如果有标记,那么我们传递,例如1,如果字段中没有设置标记,那么我们将0传递到代码Flask中)。跨度>
      猜你喜欢
      • 2016-03-16
      • 2020-12-16
      • 2022-06-10
      • 2012-06-15
      • 1970-01-01
      • 2021-10-20
      • 2013-03-12
      • 2023-01-17
      • 1970-01-01
      相关资源
      最近更新 更多