So I'm just trying to get the submit button to work properly. Work properly meaning to get the user input for email and password to be directed to my login.
Currently, it only redirects to the index.html, but I want it to go result with a redirect to either profile or error.
Here's the python part:
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in if credentials provided are correct."""
form = LoginForm(request.post)
# this is if its POST
if form.validate and request.method == 'POST':
email = request.form['email']
password = request.form['password']
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
else:
return redirect(url_for('/error'))
# this is if its GET
#return render_template("index.html", form=form)
This is the login form
class LoginForm(FlaskForm):
email = StringField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')
Here's the html part:
<div class="modal-body">
<form role="form">
<div class="form-group">
<div class="input-group">
<form method="POST" action="{{ url_for('login')}}">
{{ form.csrf }}
<dl style="width: 100%;">
<div class="form-group">
<form role="form">
<div class="form-group">
<div class="input-group">
{{ wtf.form_field(form.email) }}
{{ wtf.form_field(form.password) }}
</div>
</div> <!-- /.form-group -->
</form>
</div> <!-- /.form-group -->
<div style="margin-left: 70%;" class="checkbox">
{{ wtf.form_field(form.remember) }}
</div>
<div class="modal-footer">
<input class="btn btn-lg btn-primary btn-block" style="background-color: #3eb2a0;border-color: #3eb2a0;" type="submit" value="Sign In">
</div>
</dl>
</form>
</div>
</div>
</form>
</div>
Only problems i see with your code is one:
if email == admin@gmail.com" and password == "admin":
return redirect(url_for('/home'))
You do not have a quotation mark before admin@gmail.com
two:
return redirect(url_for('/home'))
is there a reason for the forward slash? have you tried 'home'
edit:
Here is an example of how i set up views that are similar to yours
@bp.route('/upvote', methods=['GET', 'POST'])
def view():
form = key()
if form.validate_on_submit():
received_key = form.key_code.data
url = form.url.data
username = form.username.data
return redirect(url_for('views.success')) #views is the name of the blueprint the success route is in
return render_template('upvote.html', title='title here', form=form)
form.validate_on_submit():
Takes the place of form.validate and form.submit.data/if response.method ='POST'
and you can then retrieve the data stored in the forms by form.variable.data.
Check to see if you are even receiving data back from the forms at all. It seems like it might not be recieving the post request and skipping everything under your "if request.method = 'POST'" statement.
Your view would look like:
@app.route("/login", methods=["GET", "POST"])
def login()
form = LoginForm()
# this is if its POST
if form.validate_on_submit():
email = form.email.data
password = form.password.data
if email == "admin@gmail.com" and password == "admin":
return redirect(url_for('home'))
else:
return redirect(url_for('error'))
#return render_template("index.html", form=form)