Issue
im currently learning web development with python and flask and Im trying to make a website where the user can input a number and than a python script takes that number and does something with it.
To get the input from the user I used html forms.
<body>
<div class="start">
<form action="/submit" method="post">
<input type="submit" class="start-button" value="start">
</form>
</div>
<div class="Filter-options">
<form action="/modify" method="post" class="price">
<p>Preis:</p>
<label for="option1">
<input type="text" name="options" class="price-field" value="0">
</label>
</form>
</div>
</body>
and now when the button is pressed my flask function is activated and is supposed to get the text value from my text input form.
from flask import Flask, render_template, request
from livereload import Server
import sys
sys.path.insert(0, "C:\\Users\\Myuse\\Downloads\\cellular_contract-main (1)\\cellular_contract-main")
print()
app = Flask(__name__)
@app.route('/')
def Home():
return render_template("home.html")
@app.route("/submit", methods=["POST"])
def submit():
if request.method == "POST":
text_value = request.form.get("options")
from compare import main
result = main(text_value)
return f"{result}"
# @app.route("/modify", methods=["POST"])
# def modify(text_input=None):
# if text_input is None:
# text_input = float(request.form.get("options"))
if __name__ == '__main__':
app.run(debug=True)
But i can only get the text value by pressing enter in the field and than i cant get the text value to the button function. I could do what I want without a button but I want a button.
Thx.
Solution
You have created two independent forms. One with the input field and another with the send button. However, the button must belong to the same form that you want to submit. For this reason, the fom tag must enclose both the input field and the button. Then the form with all the data in the input fields included is sent by clicking on the button.
<form action="{{ url_for('.submit') }}" method="post">
<div class="start">
<input type="submit" value="start" class="start-button" />
</div>
<div class="filter-options">
<label for="option1">Preis</label>
<input type="text" name="options" value="0" id="option1" class="price-field" />
</div>
</form>
Answered By - Detlef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.