'2 form at the same page flask
Hello guys hope u doing fine, I got this project and in home page of my website i have two form 1 to login and the other to upload a file however every time i submit with any form the same function keeps getting executed please help. this is the html code
<!--login-->
<section class="bg-dark text-light p-5 text-center">
<div class="container">
<h2>First step </h2>
<form action = "http://localhost:5000" method="post" name="login" >
<div class="col">
<div class="hide-md-lg">
<h4>Sign in to your <span class="text-warning">Linkedin account</span> </h4>
</div>
<br>
<input type="text" name="username" placeholder="Username" required>
<br><br>
<input type="password" name="password" placeholder="Password" required>
<br><br>
<input class="btn btn-primary" type="submit" value="Login">
</div>
</form>
</div>
</section>
<!--upload excel-->
<section class="bg-dark text-light p-4">
<div class="container bg-primary rounded border-0 p-5">
<div class="d-md-flex justify-content-between align-items-center">
<div class="container">
<h5 class="mt-2">Choose an excel file</h5>
<h6 class="mb-3"><span class="text-warning">NB: first 2 columns have to be: "Prénom" and "Nom"</span></h6>
</div>
<form class="input-group" action = "http://localhost:5000" method = "POST" enctype = "multipart/form-data" name="upload">
<input class="form-control size-file" type="file" name="file" id="formFile">
<input class="btn btn-dark" type = "submit" value="Start scraping" >
</form>
</div>
</div>
</section>
and this is the backend part
@app.route('/', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST' :
f = request.files['file']
f.save(secure_filename(f.filename))
df = pd.read_excel(f.filename)...............
@app.route('/', methods = ['GET', 'POST'])
def login_info():
if request.method == 'POST' and 'Sign in to your' in request.form['h4']:
user_info = request.form.get("username")
pass_info = request.form.get("password")
Solution 1:[1]
Although unable to try it this way, I updated your code with different action parameters.
<!--login-->
<section class="bg-dark text-light p-5 text-center">
<div class="container">
<h2>First step </h2>
<form action = "{{url_for('login_info')}}" method="post" name="login" >
<div class="col">
<div class="hide-md-lg">
<h4>Sign in to your <span class="text-warning">Linkedin account</span> </h4>
</div>
<br>
<input type="text" name="username" placeholder="Username" required>
<br><br>
<input type="password" name="password" placeholder="Password" required>
<br><br>
<input class="btn btn-primary" type="submit" value="Login">
</div>
</form>
</div>
</section>
<!--upload excel-->
<section class="bg-dark text-light p-4">
<div class="container bg-primary rounded border-0 p-5">
<div class="d-md-flex justify-content-between align-items-center">
<div class="container">
<h5 class="mt-2">Choose an excel file</h5>
<h6 class="mb-3"><span class="text-warning">NB: first 2 columns have to be: "Prénom" and "Nom"</span></h6>
</div>
<form class="input-group" action = "{{url_for('upload_file')}}" method = "POST" enctype = "multipart/form-data" name="upload">
<input class="form-control size-file" type="file" name="file" id="formFile">
<input class="btn btn-dark" type = "submit" value="Start scraping" >
</form>
</div>
</div>
</section>
And the @app.route should lead to two function :
@app.route('/upload_file', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST' :
f = request.files['file']
f.save(secure_filename(f.filename))
df = pd.read_excel(f.filename)...............
@app.route('/login_info', methods = ['GET', 'POST'])
def login_info():
if request.method == 'POST' and 'Sign in to your' in request.form['h4']:
user_info = request.form.get("username")
pass_info = request.form.get("password")
If you want both those page to return to your home screen, you just have to tell them to return to this same template with render_template() or redirect()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Titouan L |
