Honza Sedloň Honza Sedloň - 3 months ago 8
Javascript Question

Why "else" condition does not make, when "if" does not apply?

I wanna run different js script, when something is true and something is false. If something is True, it runs, but if it's anything else, "else" does not make. Do you know why?

Code from login.py

class Login():
def checkVal(email, password):
if db.wowband.find({"email":email, "password":password}).count() == 1:
return True
else:
return False


Code from wow.py (default script)

def hello_world():
loginForm = LoginForm()

if request.method == "POST" and loginForm.validate():
logRes = Login.checkVal(loginForm.email.data, loginForm.password.data)
print(logRes)
return render_template('index.html', loginForm=loginForm, registrationForm=registrationForm, logRes=logRes)

return render_template('index.html', loginForm=loginForm, registrationForm=registrationForm)


Code from forms.py

class LoginForm(Form):
email = EmailField('email', [validators.Email(), validators.DataRequired()])
password = PasswordField('password')
login = SubmitField('login')


And code from html file with conditions

{% if logRes %}
<script src="../static/sweetalert/dist/sweetalert.min.js"></script>
<link rel="stylesheet" href="../static/sweetalert/dist/sweetalert.css">
{% if logRes == True %}
<script>swal("Success!", "You have been logged in!", "success")</script>
{% else %}
<script>swal("Error!", "We haven't found any combination", "error")</script>
{% endif %}
{% endif %}

Answer

Try changing the last line so it sends the logRes variable to the template:

return render_template('index.html', loginForm=loginForm, registrationForm=registrationForm, logRes=logRes)

This is happening because the first line of your template which checks for the variable:

{% if logRes %}

Also, it does not makes much sense to return True or False, it is cleaner to return the comparison (which finally evaluates to True or False):

class Login():
    def checkVal(email, password):
        return db.wowband.find({"email":email, "password":password}).count() == 1