我在听米格尔先生关于烧瓶的书。 我被困在登录功能,我发送电子邮件确认给用户,然后点击链接后,它会要求用户登录,但登录后请求.args.get('next')变量为空,因此从不执行confirm函数。我怀疑这是由于before\u app_请求引起的。我对烧瓶很陌生,我花了两天的时间来解决这个问题。米格尔的版本很好,我的版本似乎被诅咒了。真的需要别人的意见。谢谢你的阅读。在
view.py
@auth.before_app_request
def before_request():
print("\tREQUEST ENDPOINT ::"+str(request.endpoint))
print("\tREQUEST ARGS ::"+str(request.args.get('next')))
if current_user.is_authenticated \
and not current_user.confirmed \
and request.endpoint \
and request.endpoint[:5] != 'auth.' \
and request.endpoint != 'static':
return redirect(url_for('auth.unconfirmed'))
@auth.route('/register', methods=['GET','POST'])
def register():
form=RegForm()
if form.validate_on_submit():
user=User(fname=form.fname.data,
lname=form.lname.data,
email=form.email.data,
organization_name=form.username.data,
password=form.password.data)
db.session.add(user)
#db.session.commit()
token=user.generate_confirmation_token()
send_email(user.email,'Confirm your Account','email/auth/confirm',user=user,token=token)
flash('A confirmation Email has been sent, Please check your inbox','success')
return redirect(url_for('auth.login'))
#except Exception as e:
# print("LINE 48 EMAIL: "+str(e))
# db.session.rollback()
return render_template('Landing/entry.html',form=form)
@auth.route('/confirm/<token>')
@login_required
def confirm(token):
if current_user.confirmed:
return redirect(url_for('viewhome.home'))
if current_user.confirm(token):
print('\tENTERED')
flash('Account confirmed ','success')
else:
flash('Invalid Link or Link has expired','Warning')
return redirect(url_for('viewhome.home'))
@auth.route('/confirm')
@login_required
def resend_confirmation():
token=current_user.generate_confirmation_token()
send_email(current_user.email,'Confirm your account','email/auth/confirm',user=current_user,token=token)
flash("A new confirmation email has been sent","success")
return redirect(url_for('viewhome.home'))
@auth.route('/unconfirmed')
def unconfirmed():
if current_user.is_anonymous or current_user.confirmed:
return redirect(url_for('viewhome.home'))
return render_template('Landing/unconfirmed.html')
@auth.route('/login',methods=['GET','POST'])
def login():
form=LoginForm()
if form.validate_on_submit():
user=User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
login_user(user,form.remember_me.data)
print("REQUEST ARGS "+str(request.args.get('next')))
return redirect(request.args.get('next') or url_for('viewhome.home'))
flash('Invalid username or password')
return render_template('Landing/login.html',form=form)
#if form.validate_on_submit():
#user=User.query.filter_by(email=form.email.data).first()
#if user is not None and User.verify_password(form.password.data)
@auth.route('/logout')
@login_required
def logout():
logout_user()
flash("You have been logged out","success")
return redirect(url_for("viewhome.home"))
^{pr2}$users.py
Landing/Login.html
{% extends "base.html" %}
{% from "_macro.html" import render_field %}
{% block title %}Login- Progressus Bootstrap template{% endblock title%}
{% block css %}
<link rel="stylesheet" href="{{url_for('static',filename='css/cust.css') }}" media="screen">
{{super()}}
{% endblock css %}
{% block page_content %}
<!--<header id="head" class="secondary"></header>-->
<!-- container -->
<div class="container">
<ol class="breadcrumb">
<li><a href="index.html">Home</a></li>
<li class="active">Registration</li>
</ol>
<div class="row">
<!-- Article main content -->
<article class="col-xs-12 maincontent">
<header class="page-header">
</header>
<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="panel panel-default">
<div class="panel-body tabs">
<ul class="tab-links clearfix">
<li class="active"><h3 class="thin">Login</h3></a></li>
</ul>
<!--<p class="text-center text-muted">Lorem ipsum dolor sit amet, <a href="signin.html">Login</a> adipisicing elit. Quo nulla quibusdam cum doloremque incidunt nemo sunt a tenetur omnis odio. </p>
-->
<hr>
<div class="tab-content">
<div id="register-form" class="tab">
<form action="{{url_for('auth.login')}}" method="post" name="register">
{{form.csrf_token}}
<div class="top-margin">
<label> Email</label>
{{ render_field(form.email,class="form-control") }}
</div>
<div class="top-margin">
<label>Password</label>
{{ render_field(form.password,class="form-control") }}
</div>
<hr>
<div class="row">
<div class="col-lg-8">
<label class="checkbox">
{{ form.remember_me }}
Keep me logged in
</label>
</div>
<div class="col-lg-4 text-right">
<!--<button class="btn btn-action" type="submit" >Register</button>-->
{{ form.submit_log(class="btn btn-action") }}
</div>
</div>
</form>
</div>
</div>
</div><!-- panel body-->
</div><!-- panel -->
</article>
<!-- /Article -->
</div>
</div>
</div> <!-- /container -->
{% endblock page_content %}
{% block script %}
{{super()}}
{% endblock script%}
我没有遵循的一点是,我们不需要在表单中设置action变量,因为这本书使用了wtf快速表单选项,所以我从未注意到它
Flask-login not redirecting to previous page
我在这件事上浪费了两天时间。我希望这能帮助那些在这件事上绊倒的人。在
相关问题 更多 >
编程相关推荐