晚上好,网站未重定向到指定页面登录
我目前正试图开发一个使用Python和Flask的网站。我发现了一些证明有用的资源,但是,当谈到为网站创建登录信息时,我遇到了困难。
我有硬编码逻辑来验证用户,如果成功将他们重定向到另一个页面(仪表板),如果不成功,用户将被提示尝试再次登录。出于某种原因,当我测试网站上的逻辑时,这些事情都不会发生。
正如你所看到的,我已经创建了一个style.css文件,但是我有兴趣知道是否有更好的方法来使用Bootstrap实现相同的结果,我打算用它来为网站的其余部分使用。我不确定是否有将此文件中的样式集成到引导框架的最佳做法。
此外,我一直在试图使用闪存功能来显示用户名和密码以进行测试,但是,一直无法使其工作。
任何帮助或见解将不胜感激。
的login.html
<link rel="stylesheet" href="/static/css/style.css" type="text/css">
{% block body %}
{% if session['logged_in'] %}
<p>You're logged in already!</p>
{% else %}
<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="email address" name="email" value="{{request.form.email}}">
<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" value="{{request.form.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" >
<br>
</div>
</div>
</div>
</form>
<p> {{error}} </p>
{% endif %}
{% endblock %}
__init__.py
from flask import Flask, flash, redirect, render_template, request, session, abort, url_for
import os
app = Flask(__name__)
@app.route('/')
def home():
if not session.get('logged_in'):
return render_template('login.html')
else:
return redirect(url_for('dashboard'))
@app.route('/dashboard/')
def dashboard():
return ("dashboard.html")
@app.route('/login/', methods=['GET','POST'])
def do_admin_login():
error = ''
try:
if request.method == "POST":
attempted_email = request.form['email']
attempted_password = request.form['password']
flash(attempted_email)
flash(attempted_password)
if attempted_password == 'password' and attempted_email == 'admin':
return redirect(url_for('dashboard'))
else:
error = "Invalid credentials. Please try again"
return render_template(login.html, error = error)
except Exception as e:
return render_template("login.html", error = error)
@app.route("/logout/")
def logout():
session['logged_in'] = False
return home()
if __name__ == "__main__":
app.run()
的style.css
* {
box-sizing: border-box;
}
*:focus {
outline: none;
}
body {
font-family: Arial;
background-color: #D3600A;
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 #D3600A;
box-shadow: none;
}
.btn {
border: 2px solid transparent;
background: #D3600A;
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;
}
嗨Mekicha。感谢您的贡献和制作答案所花费的时间。当试图实现建议1我的代码错误说第19行的缩进是一个问题。我将代码更改回原始缩进,问题已解决。关于建议2,我目睹了闪存被用来显示变量的值,因此我的论点没有被引用。我已经尝试了编码字符串,但是,这并没有。解决这个问题。你有其他的意图吗? – user2337871
在呈现模板之前尝试使用flash',看看它给出了什么。我更新了我的答案,以反映这种变化的外观 – Mekicha