Have a good day!
I would be very grateful and grateful for help on an issue that I have been thinking about for a long time.
How to display certain input fields depending on the value of variables
Flask Bootstrap's form input group multiple inputs custom forms
Input groups include support for custom selects and custom file inputs. Browser default versions of these are not supported.
I want to display a form for entries of various information. Form, for example, from 20 different elements for entering, 15-25 different fields for entering any information.
I have 25 different variables, 25 variables can take on a value, a value - either zero (0) or one (1).
Each variable corresponds to a certain field in the form for entering any information.
And I need that if one of the above variable, for example, is equal to zero (0), then I do not display the corresponding field in the form for entering any information.
The number of fields may vary. form text
<label for="inputPassword5" >Password</label>
<input type="password" id="inputPassword5" aria-describedby="passwordHelpBlock">
<div id="passwordHelpBlock" >
Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</div>
CodePudding user response:
Typically for logic similar to this you would use a Rules Based Engine. I have found a similar topic:
Without relying on one of the Rules Based Engines such as Clips, it may require a number of nested if-statements which would not be very pythonic.
Unfortunately without using one of these solutions, you may be looking to develop a service which would act as a rules engine.
You could alternatively store each input as a 0 or 1 and create a string of 25 characters similar to a bitmap, and compare each result against a dictionary like below:
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")
Then based on the items stored in the fields_to_show, you may be able to do something clever with what to display!
Hope this helps and good luck!
CodePudding user response:
Here is a million ways how to do it. You need to clarify your question and show your code.
For example:
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 1" else 'displayed' }}" for="{{ k }}">{{ k }} is</label>
<input 1" else 'displayed' }}" type="text" id="{{ k }}" name="{{ k }}"><br>
{% endfor %}
</body>
</html>
If you access URL http://127.0.0.1:5000/1?a=1&b=0&c=1
All a
, b
and c
inputs will be present inside html code, but only a
and c
will be displayed by CSS
<html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.hidden {display: none;}
</style>
</head>
<body>
<label for="a">a is</label>
<input type="text" id="a" name="a"><br>
<label for="b">b is</label>
<input type="text" id="b" name="b"><br>
<label for="c">c is</label>
<input type="text" id="c" name="c"><br>
</body></html>
If you access URL http://127.0.0.1:5000/2?a=1&b=0&c=1
The b
will not be present inside page at all
<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>