當我嘗試註冊一個用戶時,我不知道爲什麼,但是如果我試圖註冊的電子郵件不存在於db中,for循環結束,儘管有一個elif end else語句。燒瓶註冊表單條件如果elif 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>
非常感謝您的幫助!
我不明白你的邏輯:獲得數據庫中具有電子郵件X,那麼所有的行,每個在結果行中,如果電子郵件不是X,則插入數據庫。爲什麼你甚至有一個循環? – polku
要在數據庫中尋找X電子郵件地址 –
是的,我可以在沒有循環的情況下做到這一點,但沒有循環,我得到這個錯誤信息:「'NoneType'對象沒有屬性'__getitem__'」 –