javascript - 我的 ' index.py' 代码有什么问题?

标签 javascript python html mysql flask

我正在尝试使用 flask 和 mysql 创建一个 CRUD 应用程序。我尝试显示一个用于学生登录,一个用于教师登录。通过点击按钮“Std-Login”和“Teach-Login”显示表单。 当我在控制台上运行代码时,它显示 ----- 回溯(最近的通话丢失): @app.route('stdLog',methods=['POST','GET'] AssertionError: View 函数映射正在覆盖和现有端点函数:home

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>

最佳答案

因为您正在覆盖现有端点函数:home 将路由/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'

希望对你有帮助。

关于javascript - 我的 ' index.py' 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57945287/

相关文章:

javascript - 如何在表中创建效果rowspan

javascript - 我正在制作一个网站,任何人都可以将图片上传到我的网站

javascript - 如何在 DOM 中不附加此图像的情况下获取 img 高度?

javascript - 在 css 中为 &lt;input type ='radio'/> 设置边距和填充?

javascript - 在四级渐变的特定百分比处查找 RGB 颜色值

javascript - Gulp 插件 "gulp-filter"未按预期恢复流中的文件

python - 如何使用Python让网页实时显示动态数据?

python - 弃用警告 : Call to deprecated function get_sheet_by_name (Use wb[sheetname])

python - Pandas pivot_table 百分位数

javascript - 阻止在 Enter 上提交,但允许在 TextArea 中输入换行符