Flalf注册表格条件如果其他人不能工作

时间:2016-09-28 10:38:18

标签: python if-statement flask

当我尝试注册用户时,我不知道为什么,但是如果我尝试注册的电子邮件在db中不存在,则for循环结束,尽管有一个elif end else语句。

有人知道为什么吗?

这是web.py文件

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)

这是模板

<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>
    {% if success_msg %}
        <p class=success_msg><strong>Message:</strong> {{ success_msg }}
    {% endif %}
    {% if email_msg %}
        <p class=success_msg><strong>Error:</strong> {{ email_msg }}
    {% endif %}
    {% if pass_msg %}
        <p class=pass_msg><strong>Error:</strong> {{ pass_msg }}
    {% endif %}    
    <br>
    <a href="{{ url_for('index') }}">Home</a>
</div>

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

就像我在评论中所说,这不是一个编程问题,这是一个逻辑问题。

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

这将为您提供已使用此电子邮件的所有用户。所以有两种可能性:

  1. 至少有一个用户(希望您对电子邮件设置一个唯一约束,因此只有一个)使用此电子邮件。由于查询集不为空,因此您输入循环,显然您也输入了if,因为您将该行与您在请求中使用的电子邮件进行比较以获取该行。最后,你会得到预期的行为。
  2. 没有用户收到此电子邮件。那么,问题是查询集是空的,所以你不要进入循环,然后你永远不会到达你的INSERT INTO。这是逻辑问题:只有在查询集为空时才会发生插入,但如果是,则代码无法访问插入。
  3. 我不知道你如何尝试没有循环,但这应该有效:

    cur.execute("SELECT * FROM users WHERE email = (%s)", [email])
    if cur.fetchone():
        email_msg = '...'
    elif password == repeat:
        cur.execute('...')
    else:
        pass_msg = '...'