mercredi 28 septembre 2016

Flask Registration Form Condition if elif else doesn't work

When I try to register an user, I don't know why, but if the email I'm trying to register does not exist in the db, the for loop ends, though there's an elif end else statement.

Someone know why?

This is the web.py file

from flask import Flask, session, redirect, url_for, escape, request, render_template
from wtforms import Form, BooleanField, TextField, PasswordField, validators
import MySQLdb
import logging


app = Flask(__name__)


db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="usbw", port=3307, db="tourme")
cur = db.cursor()


@app.route('/register/', methods=["GET","POST"])
def register():

    email_msg = None
    success_msg = None
    pass_msg = None
    test = None

    try:

        if request.method == 'POST':

            id_id = None
            username = request.form['name']
            lastname = request.form['lastname']
            email = request.form['email']
            password = request.form['password']
            repeat = request.form['repeat']

            cur = db.cursor()
            cur.execute("SELECT * FROM users WHERE email = (%s)", [email])

            for row in cur.fetchall():
                if row[1] == email:
                    email_msg = "This e-mail already exist: " + row[1]

                elif password == repeat:
                    cur.execute("INSERT INTO users VALUES (%s,%s,%s,%s,%s)", (id_id,email,username,lastname,password))
                    db.commit()
                    success_msg = "Bravo"
                    db.close()

                else:
                    pass_msg = "Password must match"

        return render_template("register.html", email_msg=email_msg, success_msg=success_msg, pass_msg=pass_msg)

    except Exception as e:
        return(str(e)) 


app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

if __name__ == '__main__':
    app.run(debug=True)

And this is the template

<div class="register">    
    <form action="" method="POST">
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon3">Your Username</span>
            <input type="text" class="form-control" name="name" aria-describedby="basic-addon3">
        </div>
        <br>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon3">Your Lastname</span>
            <input type="text" class="form-control" name="lastname" aria-describedby="basic-addon3">
        </div>
        <br>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon3">Email</span>
            <input type="email" class="form-control" name="email" aria-describedby="basic-addon3">
        </div>
        <br>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon3">Your Password</span>
            <input type="password" class="form-control" name="password" aria-describedby="basic-addon3">
        </div>
        <br>
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon3">Repeat Password</span>
            <input type="password" class="form-control" name="repeat" aria-describedby="basic-addon3">
        </div>
        <br>
        <input type="Submit" value="Register" class="btn btn-default btn-sm">
    </form>
    
    
        
    <br>
    <a href="">Home</a>
</div>

Thanks a lot for your help!

Aucun commentaire:

Enregistrer un commentaire