登录后,Flask request.args.get('next')值消失

时间:2017-12-02 13:06:03

标签: python python-3.x flask flask-login

我正在跟随Miguel先生关于烧瓶的书。 我被困在登录功能中,我向用户发送电子邮件确认,然后点击链接后会要求用户登录但登录后请求.args.get('next')变量为空,因此永远不会执行确认功能。我怀疑这是由于before_app_request引起的。我对烧瓶很新,我花了两天时间试图解决这个问题。 Miguels版本工作正常,我似乎被诅咒。其他人的观点确实需要。谢谢你的阅读。

  

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"))
  

users.py

class User(UserMixin,db.Model):
    __tablename__ = 'users'
    id=db.Column(db.Integer,primary_key=True)
    fname=db.Column(db.String(50),nullable=False,unique=False)
    lname=db.Column(db.String(50),nullable=False,unique=False)
    organization_name=db.Column(db.String(50),nullable=False,unique=True,index=True)
    password_hash=db.Column(db.String(128),nullable=False)
    email=db.Column(db.String(30),unique=True,nullable=False,index=True)
    confirmed=db.Column(db.Boolean,default=False)
    rel=db.relationship('Domains',backref='users',lazy=True)


    #def __init__(self,fname,lname,password,email,organization_name):
    #    self.fname=fname
    #    self.lname=lname

    #    self.password=password
    #    self.email=email
    #    self.organization_name=organization_name

    @property
    def password(self):
        raise AttributeError("password is readonly")

    @password.setter
    def password(self,password):
       self.password_hash = generate_password_hash(password,method='pbkdf2:sha512',salt_length=64)


    def verify_password(self,password):
       return check_password_hash(self.password_hash, password)

    def generate_confirmation_token(self,expiration=3600):
        s=Serializer(current_app.config['SECRET_KEY'],expiration)
        return s.dumps({'confirm':self.id})

    def confirm(self,token):
        print("\tCONFIRM TOKEN FUNCTION")
        s=Serializer(current_app.config['SECRET_KEY'])
        try:
            data=s.loads(token)
        except:
            print("\tERROR HERE IN LINE 48")
            return False
        if data.get('confirm') != self.id:
            print("DEBUG:: " + int( self.id))
            return False
        print('\t\tLINE 54 REACHED')
        self.confirmed=True
        db.session.add(self)
        db.session.commit()
        return True



    def __repr__(self):
        return "<Users %r >" % self.organization_name

class Domains(db.Model):
    __tablename__='domains'
    id=db.Column(db.Integer,primary_key=True)
    user_id=db.Column(db.Integer,db.ForeignKey('users.id'),nullable=False)
    domains=db.Column(db.String(25),unique=True,nullable=False)

    def __init__(self,user_id,domain):
        self.user_id=user_id
        self.domains=domain

    def __repr__(self):
        return "<Domains %r >" % self.domains


@login_manager.user_loader
def load_user(user_id):
    print("FROM USERs :: "+user_id )
    return User.query.get(int(user_id))
  

降落/ 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%}

1 个答案:

答案 0 :(得分:1)

我没有遵循的一件事是我们不需要在表单中设置动作变量,因为本书使用了wtf快速表单选项我从未注意到它

shrinkImageFunction() {
 echo
 echo Compressing $1 . . .

 curl --user api: https://tinypng.com/developers \
      --data-binary @$1 \
      -o .$1-json \
      -s \
      https://api.tinify.com/shrink

 cat .$1-json | jq -r '.output.url' > .$1-url

 url=$(cat .$1-url)

 if [ -z "$url" ]; then
      echo Something went wrong
      return
 fi

 echo Downloading $url . . .

 echo
 curl -o compressed-$1 $url
 echo

 rm .$1-json
 rm .$1-url
}

# alias for using tinypng
alias shrinkImage=shrinkImageFunction

Flask-login not redirecting to previous page

我浪费了两天时间。我希望这可以帮助任何绊倒这个的人。