我开发了一个使用flask,jinja模板和bootstrap的网络应用程序。我想在登录后从URL中检索用户名,并将其显示在仪表板中。登录后显示名称。但是,当我单击仪表板时,它会显示“ none”(无),因为该名称已从URL中删除。如果有解决方案,请告诉我。这将非常有帮助。提前致谢。代码如下。
app.py
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
name=form.name.data
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if user.password == form.password.data:
login_user(user, remember=form.remember.data)
session['logged_in'] = True
return redirect(url_for('dashboard', name=name))
return '<h1>Invalid username or password</h1>'
#return '<h1>' + form.username.data + ' ' + form.password.data + '</h1>'
return render_template('login.html', form=form)
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
name=request.args.get('name')
return render_template('dashboard.html',name=name)
dashboard.html
<section id="home" style="background: url('../static/images/rate25.jpg');background-attachment: fixed;background-size: cover;background-repeat: no-repeat;background-position: center;">
<div class="container-fluid con" align="center">
<div class="row" align="center">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<h1 class="h1" style="font-weight: 500;text-transform: unset;">Hi {{name}}, Welcome to Ratingspro</h1></div>
<div class="col-sm-2"></div>
</div>
</div>
</section>
答案 0 :(得分:0)
如果您修改以下代码,您会在控制台中得到什么:
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
name=request.args.get('name')
print('name: {}'.format(name))
return render_template('dashboard.html',name=name)
如果一无所获,则表示名称未通过'args'传递。它可以通过其他方法(例如request.data,request.json,request.form
)传递