TypeError:'NoneType'对象在Flask,MySQL应用程序中无法下标 [英] TypeError: 'NoneType' object is not subscriptable in Flask, mysql application
本文介绍了TypeError:'NoneType'对象在Flask,MySQL应用程序中无法下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Flask-mysql创建一个登录应用程序.
I am creating a login application using Flask-mysql.
from flask import Flask, jsonify, request, json
from flask_mysqldb import MySQL
from datetime import datetime
from flask_cors import CORS
from flask_bcrypt import Bcrypt
from flask_jwt_extended import JWTManager
from flask_jwt_extended import (create_access_token, create_refresh_token, jwt_required, jwt_refresh_token_required, get_jwt_identity, get_raw_jwt)
import yaml
app = Flask(__name__)
alogin = yaml.load(open('alogin.yaml'))
app.config['MYSQL_HOST']= alogin['mysql_host']
app.config['MYSQL_USER']= alogin['mysql_user']
app.config['MYSQL_PASSWORD']= alogin['mysql_password']
app.config['MYSQL_DB']= alogin['mysql_db']
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
app.config['JWT_SECRET_KEY'] = 'secret'
mysql = MySQL(app)
bcrypt = Bcrypt(app)
jwt = JWTManager(app)
CORS(app)
@app.route('/alogin', methods=['POST'])
def login():
cur = mysql.connection.cursor()
email = request.get_json()['email']
password = request.get_json()['password']
result = ""
cur.execute("SELECT * FROM admin where email = '" + str(email) + "'")
rv = cur.fetchone()
if bcrypt.check_password_hash(rv['password'], password):
access_token = create_access_token(identity = {'id':rv['id'],'email': rv['email']})
result = jsonify({"token":access_token})
else:
result = jsonify({"error":"Invalid username and password"})
return result
if __name__ == '__main__':
app.run(debug=True)
我得到如下错误:
if bcrypt.check_password_hash(rv['password'], password):
TypeError: 'NoneType' object is not subscriptable
我尝试在其中使用'force = True'
I have tried using 'force=True' in
email = request.get_json(force = True)['email']
password = request.get_json(force = True)['password']
但是它抛出了同样的错误. 这是我对flask的第一个应用程序.帮助将不胜感激.
Yet it is throwing the same error. This is my first application with flask. Help will be appreciated.
PS:admin是数据库中表的名称,我已经在mysql数据库中创建了它.
PS : admin is the name of the table in the database and I have already created it in the mysql database.
推荐答案
fetchone()
将返回None
.添加
if rv is None:
检查以解决有人提供不在数据库中的电子邮件地址的情况.
check to handle the case of someone providing an email address that isn't in the database.
还要花点时间查看"SQL注入攻击".
Also, take a moment and look up "SQL Injection Attack".
这篇关于TypeError:'NoneType'对象在Flask,MySQL应用程序中无法下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文