I am new to this but I have already done quite a bit in PythonAnywhere, but I have a few questions. This question is different from Flask - POST Error 405 Method Not Allowed as it does not go into detail and depth.
1) How do I know if my forms.py is validating correctly with my database?
2) How do I know if I am using my database?
3) Does the submit button submit a POST method?
4) Where is the form.something() coming from? It is not imported from anywhere.
5) Does the session need to match the input names in the html?
routes.py
mysql = MySQL()
mysql.init_app(app)
@app.route("/", methods = ['GET', 'POST'])
def login():
if 'email' in session:
return redirect(url_for('update_user'))
elif 'avatarName' in session:
return redirect(url_for('update_user'))
form = LoginForm()
if request.method == "POST":
if form.validate() == False:
return render_template("login.html", form=form)
else:
avatarName = form.avatarName.data
email = form.email.data
password = form.password.data
user_avatar = mysql.users.query.filter_by(avatarName=avatarName).first()
user_email = mysql.users.query.filter_by(email=email).first()
user_password = mysql.users.query.filter_by(password=password).first()
if user_email is not None and user_password is not None:
session['email'] = form.email.data
return redirect(url_for('update_user'))
elif user_avatar is not None and user_password is not None:
session['avatarName'] = form.avatarName.data
return redirect(url_for('update_user'))
else:
return redirect(url_for('login'))
elif request.method == 'GET':
return render_template('login.html', form=form)
@app.route("/update_user", methods = ['GET', 'POST'])
def update_user():
return render_template("update.html")
forms.py
class LoginForm(Form):
avatarName = StringField(validators=[InputRequired()])
email = EmailField(validators=[InputRequired(), Email()])
password = PasswordField(validators=[InputRequired()])
submit = SubmitField()
login.html
<form action="/update_user" method="post">
<div>
<label>Username or Email</label>
<input type="text" class="text-input" placeholder="Enter Username or Email" name="avatarName" value="{{request.form.avatarName}}">
</div>
<div>
<label>Password</label>
<input type="password" class="text-input" placeholder="Enter Password" name="password" value="{{request.form.password}}">
</div>
<div class="links">
<a href="#">Forgot password?</a>
</div>
<div class="checkbox">
<input type="checkbox" class="check-input"> Remember Me <br>
</div>
<button name="login" type="submit" class="primary-btn">Sign In</button>
</form>
I don't really know why it is giving me a 405 error. It is not giving me the /update_user page. If I just type in the URL, it works so it's not the URL problem. When I click the submit button it but validate the text fields and just brings me to the update_user page.
UPDATE:
- After testing around with the code, I think the validation is being skipped so how do I implement my forms.py?