title: Flask request.args.get('next') value disappears after logging in
body: <p>I'm following Miguel sir's book on flask.
  I'm stuck in the login function where I send the email confirmation to the user and then after clicking the link it will ask the user to login but after logging in the request.args.get('next') variable is empty hence the confirm function is never executed. I suspect this is caused due to the before_app_request.  I'm very new to flask and I've spent two days trying to fix this. Miguels version works fine, mine seem's to be cursed. Someone else's view is really needed. Thanks for reading.</p>
  
  <blockquote>
    <p>view.py</p>
  </blockquote>
  
  <pre><code>@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/&lt;token&gt;')
  @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"))
  </code></pre>
  
  <blockquote>
    <p>users.py</p>
  </blockquote>
  
  <pre><code>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 "&lt;Users %r &gt;" % 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 "&lt;Domains %r &gt;" % self.domains
  
  
  @login_manager.user_loader
  def load_user(user_id):
      print("FROM USERs :: "+user_id )
      return User.query.get(int(user_id))
  </code></pre>
  
  <blockquote>
    <p>Landing/Login.html</p>
  </blockquote>
  
  <pre><code>{% extends "base.html" %}
  {% from "_macro.html" import render_field %}
  
      {% block title %}Login- Progressus Bootstrap template{% endblock title%}
  {% block css %}
  &lt;link rel="stylesheet" href="{{url_for('static',filename='css/cust.css') }}" media="screen"&gt;
  
  
  
  {{super()}}
  {% endblock css %}
  
  
  
  {% block page_content %}
  
  &lt;!--&lt;header  id="head" class="secondary"&gt;&lt;/header&gt;--&gt;
  
      &lt;!-- container --&gt;
      &lt;div class="container"&gt;
  
          &lt;ol class="breadcrumb"&gt;
              &lt;li&gt;&lt;a href="index.html"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li class="active"&gt;Registration&lt;/li&gt;
          &lt;/ol&gt;
  
          &lt;div class="row"&gt;
  
              &lt;!-- Article main content --&gt;
              &lt;article class="col-xs-12 maincontent"&gt;
              &lt;header class="page-header"&gt;
  
                  &lt;/header&gt;
  
                  &lt;div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2"&gt;
  
                      &lt;div class="panel panel-default"&gt;
                          &lt;div class="panel-body tabs"&gt;
  
                              &lt;ul class="tab-links clearfix"&gt;
                                  &lt;li class="active"&gt;&lt;h3 class="thin"&gt;Login&lt;/h3&gt;&lt;/a&gt;&lt;/li&gt;
                              &lt;/ul&gt;
  
                              &lt;!--&lt;p class="text-center text-muted"&gt;Lorem ipsum dolor sit amet, &lt;a href="signin.html"&gt;Login&lt;/a&gt; adipisicing elit. Quo nulla quibusdam cum doloremque incidunt nemo sunt a tenetur omnis odio. &lt;/p&gt;
                              --&gt;
                              &lt;hr&gt;
  
                              &lt;div class="tab-content"&gt;
  
                          &lt;div id="register-form" class="tab"&gt;
                              &lt;form action="{{url_for('auth.login')}}" method="post" name="register"&gt;
                                  {{form.csrf_token}}
                                  &lt;div class="top-margin"&gt;
                                      &lt;label&gt; Email&lt;/label&gt;
                                         {{ render_field(form.email,class="form-control") }}
                                     &lt;/div&gt;
                                     &lt;div class="top-margin"&gt;
                                         &lt;label&gt;Password&lt;/label&gt;
                                         {{ render_field(form.password,class="form-control") }}
                             &lt;/div&gt;
  
  
                             &lt;hr&gt;
  
                             &lt;div class="row"&gt;
                                 &lt;div class="col-lg-8"&gt;
                                  &lt;label class="checkbox"&gt;
  
                                         {{ form.remember_me }}
                                         Keep me logged in
                                     &lt;/label&gt;
                                 &lt;/div&gt;
                                 &lt;div  class="col-lg-4 text-right"&gt;
  
                                     &lt;!--&lt;button class="btn btn-action" type="submit" &gt;Register&lt;/button&gt;--&gt;
  
                                         {{ form.submit_log(class="btn btn-action") }}
  
                                 &lt;/div&gt;
                             &lt;/div&gt;
                         &lt;/form&gt;
                     &lt;/div&gt;
  
                      &lt;/div&gt;
  
                      &lt;/div&gt;&lt;!-- panel body--&gt;
  
                  &lt;/div&gt;&lt;!-- panel --&gt;
  
              &lt;/article&gt;
              &lt;!-- /Article --&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;  &lt;!-- /container --&gt;
  
  
  {% endblock page_content %}
  {% block script %}
  {{super()}}
  
  
  {% endblock script%}
  </code></pre>