python - 在 FLASK WEBFRAMEWORK 中获取 POST 请求错误 405
问题描述
我已经查看了与此相关的问题的答案,但即便如此,我的问题仍然存在。基本上我正在烧瓶中制作登录页面。记录存在于我的数据库中我还在 html、app 中使用了 method = POST 但即使这样我也收到此错误输出: 输出
HTML:
<form method="POST">
<div class="row">
<h2 style="text-align:center; font-family:Courier New; color:#468E85" > Welcome this will be amazing experience:) </h2>
<div class="vl">
<span class="vl-innertext">or</span>
</div>
<div class="col">
<a href="https://web.facebook.com/" class="fb btn">
<i class="fa fa-facebook fa-fw"></i> Login with Facebook
</a>
<a href="https://twittermobile..com/login" class="twitter btn">
<i class="fa fa-twitter fa-fw"></i> Login with Twitter
</a>
<a href="https://myaccount.google.com/" class="google btn"><i class="fa fa-google fa-fw">
</i> Login with Google+
</a>
</div>
<div class="col">
<div class="hide-md-lg">
</div>
<label for="username">Username</label>
<input type="" id="username" name="username" placeholder="Username" value="{{
request.form.username }}" required>
<label for="psw">Password</label>
<input type="password" id="psw" name="psw" pattern="(?=. \d)(?=. [az])(?=.*[AZ]).{8,}" title="必须包含至少一个数字和一个大小写字母,至少8个或更多字符" value="{{ request.form.password }}"required>
<button type="submit" class="btn form-control btn-default">Login</button>
<button type="button" class="btn btn-primary">Sign Up</button>
<div class="msg">{{ msg }}</div>
<button class="submit" style="color: #008080">Forgot password?</button>
</div>
</div>
应用程序.py
@app.route('/')
@app.route('/loginform' , methods= ['GET','POST'])
def loginform():
# Check if "username" and "password" POST requests exist (user submitted form)
print("herehere")
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
# Create variables for easy access
print("Hellooo")
username = request.form['username']
password = request.form['password']
#Check if account exists using MySQL
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password,))
# Fetch one record and return result
account = cursor.fetchone()
if account is not None:
# Create session data, we can access this data in other routes
session['loggedin'] = True
session['id'] = account['id']
session['username'] = account['username']
# Redirect to home page
print("I am called")
return redirect('Quickshop.html')
else:
print("now i am here")
# Account doesnt exist or username/password incorrect
msg = 'Incorrect username/password!'
return render_template('loginform.html',msg = msg)
解决方案
您的 from 标记中的显式操作属性,并且您没有分配值。
尝试这个:-
<form method="POST" action="/loginform">
<input type="text" placeholder="Username" name="username" />
<input type="password" placeholder="Password" name="password" />
<input type="submit" value="Login" />
</form>
您可以在 request.form 中找到密码和用户名。
推荐阅读
- python - 如何在 DEAP 中设置个体基因的上限和下限?
- sql - SQL Server JSON 查询过滤返回
- mysql - 如何使用连接包含一个完整的列
- angular - 如何测试一个复杂的可观察链?
- twilio - 使用 Authy API 时,有没有办法为用户更新 OTP 密码?
- ruby - 使用 CL var 执行哈希查找
- python - 在python中实现if语句后如何继续
- bootstrap-4 - 导航栏切换器在重定向到新页面时不会折叠
- php - 在 preg_match php 中排除单引号
- python - Django:修改基于类的视图上下文(使用 **kwargs)