我正在尝试使用flask和mysql创建CRUD应用程序。我尝试显示一个用于学生登录,一个用于tteacher登录。点击按钮“Std Login”和“Teach Login”显示表单。
当我在控制台上运行代码时,它显示-----
回溯(最近一次呼叫丢失):
@app.route('stdLog',方法=['POST','GET']
Apple错误:View函数映射是重写和现有的端点函数:首页
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)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button onclick="mystd()">Std-Login</button>
<button onclick="myteach()">Teach-Login</button>
<div id="std" align="center">
<form method="post" action="/stdLog" >
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" name="submit" value="std">
</form>
</div>
<br>
<br>
<!-- <div id="teach" >
<form method="post" action="/teacherLog" >
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" name="submit" value="teacher">
</form>
</div> -->
</body>
<script type="text/javascript">
function mystd()
{
var s=document.getElementById("std");
if(s.style.display == "none"){
s.style.display = "block";
}else{
s.style.display = "none";
}
}
function myteach()
{
var t=document.getElementById("teach");
if(t.style.display == "none"){
t.style.display = "block";
}else{
t.style.display = "none";
}
}
</script>
</html>
最佳答案
因为您正在重写和现有的端点函数:首页
将route/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'
希望这对你有帮助。