Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python flask登录页键错误_Python_Flask_Mysql Python_Flask Login - Fatal编程技术网

python flask登录页键错误

python flask登录页键错误,python,flask,mysql-python,flask-login,Python,Flask,Mysql Python,Flask Login,我已经使用python编写了登录、注册和联系的代码。我收到一个密钥错误“0” 当我尝试登录时,在联系人中出现了一个KeyError:“UserId”。我使用的是remoteMysql,当用户尝试联系时,我还使用smtp向管理员发送邮件。 这是我的apps.py @app.route('/',methods =['GET', 'POST']) def Registration(): msg = '' if request.method =="POST":

我已经使用python编写了登录、注册和联系的代码。我收到一个密钥错误“0” 当我尝试登录时,在联系人中出现了一个KeyError:“UserId”。我使用的是remoteMysql,当用户尝试联系时,我还使用smtp向管理员发送邮件。 这是我的apps.py

@app.route('/',methods =['GET', 'POST'])
def Registration():
    msg = ''
    if request.method =="POST":
        name = request.form["name"]
        email = request.form["email"]
        password = request.form["password"]
        cursor = mysql.connection.cursor()
        cursor.execute('SELECT * FROM tableone WHERE name = % s', (name, ))
        account = cursor.fetchone()
        print(account)
        if account:
            msg = 'Account already exists !'
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
            msg = 'Invalid email address !'
        elif not re.match(r'[A-Za-z0-9]+', name):
            msg = 'name must contain only characters and numbers !'
        else:
            cursor.execute('INSERT INTO tableone VALUES(NULL,% s,% s,% s)',(name,email,password))
            mysql.connection.commit()
            msg = "you have sucessfully got registered"
            TEXT = "Hello "+name + ",\n\n"+ """Thanks for applying registring at smartinterns """
            message  = 'Subject: {}\n\n{}'.format("smartinterns Carrers", TEXT)
            sendmail(TEXT,email)
            #sendgridmail(email,TEXT)
    elif request.method == 'POST':
        msg = 'Please fill out the form !'
    return render_template("Registration.html",msg = msg)

@app.route('/login',methods=['GET', 'POST'])
def Login():
    global UserId
    msg = ''
    if request.method == 'POST' and 'name' in request.form and 'password' in request.form:
        name = request.form['name']
        password = request.form['password']
        # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM tableone WHERE name = %s AND password = %s', (name, password,))
        # Fetch one record and return result
        account = cursor.fetchone()
        print (account)
        # If account exists in accounts table in out database
        if account:
            # Create session data, we can access this data in other routes
            session['loggedin'] = True
            session['id'] = account[0]
            UserId = account[0]
            session['name'] = account[1]
            # Redirect to home page
            msg = 'Logged in successfully!'
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    # Show the login form with message (if any)
    return render_template('Login.html', msg=msg)
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/contact',methods=['GET','POST'])
def contact():
    msg=' '
    if request.method =="POST":
        name = request.form['name']
        email = request.form['email']
        subject= request.form['subject']
        message = request.form['message']
        cursor = mysql.connection.cursor()
        cursor.execute('SELECT * FROM contact WHERE id = % s', (session['UserId'],))
        #account = cursor.fetchone()
        #print(account)
        cursor = mysql.connection.cursor()
        cursor.execute('INSERT INTO contact VALUES (% s, % s, % s, % s,% s)', (session['UserId'],name, email,subject,message))
        mysql.connection.commit()
        msg = 'You have successfully applied for job !'
        session['loggedin'] = True
        TEXT = " "+message+"from"+email+" "
        sendmail(TEXT,"kavyapadala259@gmail.com")
        #sendgridmail("kavyapadala259@gmail.com",TEXT)
    elif request.method == 'POST':
        msg = 'Please fill out the form !'
    return render_template('contact.html', msg = msg)
remoteMysql包含 tableone包含用户ID、名称、电子邮件和密码字段
联系人字段id、姓名、电子邮件、主题、消息这是一本字典。因此,我们编写帐户(“UserId”)而不是帐户[0]。这将解决错误keyerror:“0”。使用键而不是索引号进行抓取。

会话中不存储
UserId
,但存储的值与
id