我的“ index.py”代码有什么问题?

时间:2019-09-15 14:57:25

标签: javascript python html mysql flask

我正在尝试使用flask和mysql创建一个CRUD应用程序。我试图显示为学生登录的一个和tteacher登录的一个。点击“ Std-Login”和“ Teach-Login”按钮即可显示该表格。 当我在控制台上运行代码时,它显示----- 追溯(最近的呼叫丢失): @ app.route('stdLog',methods = ['POST','GET'] AssertionError:view函数映射被覆盖,现有的终结点函数:home

max_result = None
max_x = 0
max_y = 0
for x in range(0, 101):
    for y in range(0, 101):
       result = fun_A(x, y)

       if max_result is None or result > max_result:
           max_result = result
           max_x = x
           max_y = y

print(f"x={max_x} and y={max_y} produced the maximum result of {max_result}")
from flask import Flask,session,render_template,request
import os
import mysql.connector
from flask_bcrypt import Bcrypt

app=Flask(__name__)
# app.secret_key=os.urandom(24)
bcrypt=Bcrypt(app)


mydb=mysql.connector.connect(
	host="localhost",
	user="root",
	password="",
	db="mydatabase"
 )



@app.route('/')
def home():
	return render_template('home.html')






@app.route('/stdLog',methods=['POST','GET'])
def home():
	if request.method=='POST':
		mycursor=mydb.cursor()
		pwHash=bcrypt.generate_password_hash(request.form['password'])
		sql="insert into student (name,password) values (%s,%s)"
		val=(request.form['name'],pwHash)
		mycursor.execute(sql,val)
		mydb.commit()
		print(mycursor.rowcount,"student's record inserted")
	return 'ok'



@app.route('/teacherLog',methods=['POST','GET'])
def teacher():
	if request.method == 'POST':
		mycursor=mydb.cursor()
		pwHash=bcrypt.generate_password_hash(request.form['tpassword'])
		sql="insert into teacher(tname,tpassword) values(%s,%s)"
		val=(request.form['name'],pwHash)
		mycursor.execute(sql,val)
		mydb.commit()
		print(mycursor.rowcount,"teacher's recored Inserted")
	return 'Inserted'


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

1 个答案:

答案 0 :(得分:1)

因为您要覆盖现有的端点功能:主页 将路由/ stdLog功能更改为:def home2():def stdLog():

 @app.route('/stdLog',methods=['POST','GET'])
    def stdLog():
        if request.method=='POST':
            mycursor=mydb.cursor()
            pwHash=bcrypt.generate_password_hash(request.form['password'])
            sql="insert into student (name,password) values (%s,%s)"
            val=(request.form['name'],pwHash)
            mycursor.execute(sql,val)
            mydb.commit()
            print(mycursor.rowcount,"student's record inserted")
        return 'ok'

希望这对您有所帮助。