我使用flask框架在python中编写代码来构建应用程序,但在重定向到下一页时显示内部服务器错误。 登录页面包含将重定向到下一个路径 page.so如何解决该问题。 这是app.py文件的代码。
from flask import Flask, request, render_template
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'invertoryDb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
MYSQL_DATABASE_SOCKET = '/tmp/mysql.sock'
mysql.init_app(app)
@app.route('/')
def my_form():
return render_template('loginPage.html')
@app.route('/login', methods=['POST'])
def Authenticate():
username = request.form['username']
password = request.form['password']
cursor = mysql.connect().cursor()
cursor.execute("SELECT * FROM user WHERE username='"+username+"' and password='"+password+"'")
data = cursor.fetchone()
if data is None:
return "usernme and password is wrong"
else:
return "logged in succesfully"
if __name__ == '__main__':
app.run()
这是html文件的代码。
<!DOCTYPE html>
<html>
<head>
<title>Login page</title>
<style type="text/css">
* {
box-sizing: border-box;
}
*:focus {
outline: none;
}
body {
font-family: Arial;
background-color: #3498DB;
padding: 50px;
}
.login {
margin: 20px auto;
width: 300px;
}
.login-screen {
background-color: #FFF;
padding: 20px;
border-radius: 5px
}
.app-title {
text-align: center;
color: #777;
}
.login-form {
text-align: center;
}
.control-group {
margin-bottom: 10px;
}
input {
text-align: center;
background-color: #ECF0F1;
border: 2px solid transparent;
border-radius: 3px;
font-size: 16px;
font-weight: 200;
padding: 10px 0;
width: 250px;
transition: border .5s;
}
input:focus {
border: 2px solid #3498DB;
box-shadow: none;
}
.btn {
border: 2px solid transparent;
background: #3498DB;
color: #ffffff;
font-size: 16px;
line-height: 25px;
padding: 10px 0;
text-decoration: none;
text-shadow: none;
border-radius: 3px;
box-shadow: none;
transition: 0.25s;
display: block;
width: 250px;
margin: 0 auto;
}
.btn:hover {
background-color: #2980B9;
}
.login-link {
font-size: 12px;
color: #444;
display: block;
margin-top: 12px;
}
</style>
</head>
<body>
<form action="/login" method="POST">
<div class="login">
<div class="login-screen">
<div class="app-title">
<h1>Login</h1>
</div>
<div class="login-form">
<div class="control-group">
<input type="text" class="login-field" value="" placeholder="username"
name="username">
<label class="login-field-icon fui-user" for="login-name"></label></div>
<div class="control-group">
<input type="password" class="login-field" value=""
placeholder="password" name="password">
<label class="login-field-icon fui-lock" for="login-pass"></label></div>
<input type="submit" value="Log in" class="btn btn-primary btn-large btn-block">
</div>
</div>
</div>
</form>
</body>
</html>
这是应用程序的运行控制台。(错误列表)
127.0.0.1 - - [03/Feb/2020 11:58:41] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Feb/2020 11:58:41] "GET / HTTP/1.1" 200 -
[2020-02-03 11:58:54,752] ERROR in app: Exception on /login [POST]
Traceback (most recent call last):
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\app.py", line 2446, in
wsgi_app
response = self.full_dispatch_request()
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\app.py", line 1951, in
full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\app.py", line 1820, in ha
ndle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\_compat.py", line 39, in
reraise
raise value
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\app.py", line 1949, in
full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flask\app.py", line 1935, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\OWNER\PycharmProjects\untitled\app.py", line 27, in Authenticate
cursor = mysql.connect().cursor()
File "C:\Users\OWNER\PycharmProjects\untitled\venv\lib\site-packages\flaskext\mysql.py", line 54, in
connect
if self.app.config['MYSQL_DATABASE_SOCKET']:
KeyError: 'MYSQL_DATABASE_SOCKET'
127.0.0.1 - - [03/Feb/2020 11:58:54] "POST /login HTTP/1.1" 500 -
答案 0 :(得分:1)
您需要在应用配置中定义MYSQL_DATABASE_SOCKET
,而不是在文件中定义为静态变量。
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'invertoryDb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_SOCKET'] = '/tmp/mysql.sock'
MYSQL_DATABASE_SOCKET
没有默认值,因此必须在初始化时指定它。在这种情况下,初始化没有发生,所以您遇到了关键错误。
此外,如果您将配置指定为app.config['MYSQL_DATABASE_SOCKET'] = None
而不是/tmp/mysql.sock
,它将加载默认的unix套接字。