[Flask] Flask Digest, Vol 49, Issue 2

Abdul Mohammed imonikemohammed at gmail.com
Tue Jul 2 11:34:34 EDT 2019


Hello
Thanks Leni, Scott for replying. I had tried that previously.
user.is_authenticated cannot be set. It throws an error.
I had stumbled upon a tutorial where user.authenticated had been set and so
thought that was the proper way to modify the value
of user.is_authenticated. When I set user.authenticated in the code, the
app didn't throw any errors which further bolstered my
previous beliefs. It's just that setting it seems to have no effect or its
effect is reversed by the time login_user(user) logs you in to
the next view. Just after setting user.authenticated to True, I issue a
print statement to check the the value user.is_authenticated
and it shows True. By the time login_user(user) logs me in, I issue another
print statement and user.is_authenticated has reverted
to False.

On Mon, Jul 1, 2019 at 11:15 PM <flask-request at python.org> wrote:

> Send Flask mailing list submissions to
>         flask at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/flask
> or, via email, send a message with subject or body 'help' to
>         flask-request at python.org
>
> You can reach the person managing the list at
>         flask-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Flask digest..."
>
>
> Today's Topics:
>
>    1. Re: Unable to set current_user.authenticated to True
>       (Leni Kadali Mutungi)
>    2. Re: Flask Digest, Vol 49, Issue 1 (Unable to set
>       current_user.authenticated to True) (sidwoodstock .)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 1 Jul 2019 20:03:04 +0300
> From: Leni Kadali Mutungi <lenikmutungi at gmail.com>
> To: flask at python.org
> Subject: Re: [Flask] Unable to set current_user.authenticated to True
> Message-ID: <177572e2-a71d-a250-e6ec-defcce5b5161 at gmail.com>
> Content-Type: text/plain; charset=utf-8; format=flowed
>
> I think you mean to set it as `current_user.is_authenticated = True`.
>
> On 7/1/19 6:14 PM, Abdul Mohammed wrote:
> > Hello everyone,
> > ????????????????? I am trying to toggle between "Sign in" and "Sign
> > out" links using the current_user.is_authenticated variable such that
> > when it returns False, the "Sign in" is displayed and when True the
> > "Sign out" link is displayed. At some point in the login code I set
> > current_user.authenticated = True. The thing is that by the I am
> > successfully logged?in and presented with the next view, it is the
> > "Sign in" link that is still displayed. When I check
> > current_user.is_authenticated, it returns False, even though I had
> > explicitly set it to True. Please can anyone point out what I am might
> > be doing?wrong? Here is my code:
> >
> > I have a base.html file that contains the "Sign in" and "Sign out"
> > links that is extended by the other templates:
> >
> > <ul class="nav navbar-nav navbar-right">
> > ?? {% if (current_user.is_authenticated) %}
> > ?? <li><a href="{{ url_for('logout') }}">Sign Out</a></li>
> > ?? {% else %}
> > ?? <li><a href="{{ url_for('index') }}">Sign In</a></li>
> > ?? {% endif %}
> > </ul>
> >
> > <body>
> > {% block page_content %}{% endblock %}
> > </body>
> >
> >
> > ?When the app first loads in the browser it displays a login form,
> > login.html:
> >
> > ?{% extends "base.html" %}
> >
> > {% block content %}
> > ??? {% import "bootstrap/wtf.html" as wtf %}
> > ??? {% block title %}Flasky - Login{% endblock %}
> > ??? {% block page_content %}
> > ??? <div class="page-header">
> > ??? ??? <h1>Login</h1>
> > ??? </div>
> > ??? <div class="col-md-4">
> > ??? ??? {{ wtf.quick_form(form) }}
> > ??? </div>
> > ??? {% endblock %}
> > {% endblock %}
> >
> > When a user logs in, it loads a template dashboard.html that displays
> > a map. The top part looks like this:
> >
> > {% extends "base.html" %}
> >
> > {% block page_content %}
> > <html>
> > ?<head>
> > ?? <title>Lagos Map</title>
> > ?? <link rel="stylesheet"
> > href="https://unpkg.com/leaflet@1.1.0/dist/leaflet.css"
> >
> integrity="sha512-wcw6ts8Anuw10Mzh9Ytw4pylW8+NAD4ch3lqm9lzAsTxg0GFeJgoAtxuCLREZSC5lUXdVyo/7yfsqFjQ4S+aKw=="
> > ?? crossorigin=""/>
> > ?? <script src="https://unpkg.com/leaflet@1.1.0/dist/leaflet.js"
> >
> integrity="sha512-mNqn2Wg7tSToJhvHcqfzLMU6J4mkOImSPTxVZAdo+lcPlk+GhZmYgACEe0x35K7YzW1zJ7XyJV/TT1MrdXvMcA=="
> > ?? crossorigin=""></script>
> > ?? <script
> > src="
> https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.js
> "></script>
> > ?? <link rel="stylesheet"
> > href="
> https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.css"
>
> > />
> > ?? <link rel="stylesheet"
> > href="
> https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.css"
>
> > />
> > ?? <script
> > src="
> https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.js
> "></script>
> > ?? <script
> > src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js
> "></script>
> > ? ...
> > ? ...
> >
> > And these are the relevant routes:
> >
> > @app.route('/',methods=['GET','POST'])
> > def index():
> > ?? ?? form = LoginForm()
> > ????? if form.validate_on_submit():
> > ??? ????? user = User.query.filter_by(email=form.email.data).first()
> > ??? ????? if user is not None and
> user.check_password(form.password.data):
> > ??? ??? ????? user.authenticated = True
> > ??? ??? ????? current_user = user
> > ??? ??? ????? db.session.add(current_user)
> > ??? ??? ????? db.session.commit()
> > ??? ??? ????? login_user(current_user, form.remember_me.data)
> > ??? ??? ?? ?? return redirect(request.args.get('next') or
> > url_for('dashboard'))
> > ??? ????? flash('Invalid username or password.')
> > ?????? return render_template('auth/login.html', form=form)
> >
> > @app.route('/dashboard')
> > def dashboard():
> > ????? return render_template("dashboard.html")
> >
> > Thank you for your time.
> >
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
>
> --
> -- Kind regards,
> Leni Kadali Mutungi
>
>
>
> ------------------------------
>
> Message: 2
> Date: Mon, 1 Jul 2019 15:14:24 -0700
> From: "sidwoodstock ." <sidwoodstock at gmail.com>
> To: flask at python.org
> Subject: Re: [Flask] Flask Digest, Vol 49, Issue 1 (Unable to set
>         current_user.authenticated to True)
> Message-ID:
>         <
> CALovGGPvjb8Zh+BKOx6kj7+ZQ71fj1BJv5WRYbrxxijyFnMKgQ at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> At a cursory glance I would say that user.is_authenticated is not the same
> as user.authenticated..
>
> Are you using Flask-Login?
>
> -Scott Woodstock
>
> On Mon, Jul 1, 2019 at 9:01 AM <flask-request at python.org> wrote:
>
> > Send Flask mailing list submissions to
> >         flask at python.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >         https://mail.python.org/mailman/listinfo/flask
> > or, via email, send a message with subject or body 'help' to
> >         flask-request at python.org
> >
> > You can reach the person managing the list at
> >         flask-owner at python.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of Flask digest..."
> >
> >
> > Today's Topics:
> >
> >    1. Unable to set current_user.authenticated to True (Abdul Mohammed)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Mon, 1 Jul 2019 16:14:33 +0100
> > From: Abdul Mohammed <imonikemohammed at gmail.com>
> > To: flask at python.org
> > Subject: [Flask] Unable to set current_user.authenticated to True
> > Message-ID:
> >         <CAEKkz86wtwLtPni2LML6iM6WW46sHQxq3P=
> > 8j1pq2hW1ZdAVHA at mail.gmail.com>
> > Content-Type: text/plain; charset="utf-8"
> >
> > Hello everyone,
> >                   I am trying to toggle between "Sign in" and "Sign out"
> > links using the current_user.is_authenticated variable such that when it
> > returns False, the "Sign in" is displayed and when True the "Sign out"
> link
> > is displayed. At some point in the login code I set
> > current_user.authenticated = True. The thing is that by the I am
> > successfully logged in and presented with the next view, it is the "Sign
> > in" link that is still displayed. When I check
> > current_user.is_authenticated, it returns False, even though I had
> > explicitly set it to True. Please can anyone point out what I am might be
> > doing wrong? Here is my code:
> >
> > I have a base.html file that contains the "Sign in" and "Sign out" links
> > that is extended by the other templates:
> >
> > <ul class="nav navbar-nav navbar-right">
> >    {% if (current_user.is_authenticated) %}
> >    <li><a href="{{ url_for('logout') }}">Sign Out</a></li>
> >    {% else %}
> >    <li><a href="{{ url_for('index') }}">Sign In</a></li>
> >    {% endif %}
> > </ul>
> >
> > <body>
> > {% block page_content %}{% endblock %}
> > </body>
> >
> >
> >  When the app first loads in the browser it displays a login form,
> > login.html:
> >
> >  {% extends "base.html" %}
> >
> > {% block content %}
> >     {% import "bootstrap/wtf.html" as wtf %}
> >     {% block title %}Flasky - Login{% endblock %}
> >     {% block page_content %}
> >     <div class="page-header">
> >         <h1>Login</h1>
> >     </div>
> >     <div class="col-md-4">
> >         {{ wtf.quick_form(form) }}
> >     </div>
> >     {% endblock %}
> > {% endblock %}
> >
> > When a user logs in, it loads a template dashboard.html that displays a
> > map. The top part looks like this:
> >
> > {% extends "base.html" %}
> >
> > {% block page_content %}
> > <html>
> >  <head>
> >    <title>Lagos Map</title>
> >    <link rel="stylesheet" href="
> > https://unpkg.com/leaflet@1.1.0/dist/leaflet.css"
> >
> >
> >
> integrity="sha512-wcw6ts8Anuw10Mzh9Ytw4pylW8+NAD4ch3lqm9lzAsTxg0GFeJgoAtxuCLREZSC5lUXdVyo/7yfsqFjQ4S+aKw=="
> >    crossorigin=""/>
> >    <script src="https://unpkg.com/leaflet@1.1.0/dist/leaflet.js"
> >
> >
> >
> integrity="sha512-mNqn2Wg7tSToJhvHcqfzLMU6J4mkOImSPTxVZAdo+lcPlk+GhZmYgACEe0x35K7YzW1zJ7XyJV/TT1MrdXvMcA=="
> >    crossorigin=""></script>
> >    <script src="
> >
> >
> https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.js
> > "></script>
> >    <link rel="stylesheet" href="
> >
> >
> https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.css
> > "
> > />
> >    <link rel="stylesheet" href="
> >
> >
> https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.css
> > "
> > />
> >    <script src="
> >
> >
> https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.js
> > "></script>
> >    <script src="
> > https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
> >   ...
> >   ...
> >
> > And these are the relevant routes:
> >
> > @app.route('/',methods=['GET','POST'])
> > def index():
> >       form = LoginForm()
> >       if form.validate_on_submit():
> >           user = User.query.filter_by(email=form.email.data).first()
> >           if user is not None and
> user.check_password(form.password.data):
> >               user.authenticated = True
> >               current_user = user
> >               db.session.add(current_user)
> >               db.session.commit()
> >               login_user(current_user, form.remember_me.data)
> >               return redirect(request.args.get('next') or
> > url_for('dashboard'))
> >           flash('Invalid username or password.')
> >        return render_template('auth/login.html', form=form)
> >
> > @app.route('/dashboard')
> > def dashboard():
> >       return render_template("dashboard.html")
> >
> > Thank you for your time.
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> > URL: <
> >
> http://mail.python.org/pipermail/flask/attachments/20190701/9674ee57/attachment-0001.html
> > >
> >
> > ------------------------------
> >
> > Subject: Digest Footer
> >
> > _______________________________________________
> > Flask mailing list
> > Flask at python.org
> > https://mail.python.org/mailman/listinfo/flask
> >
> >
> > ------------------------------
> >
> > End of Flask Digest, Vol 49, Issue 1
> > ************************************
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/flask/attachments/20190701/9252979c/attachment.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
> ------------------------------
>
> End of Flask Digest, Vol 49, Issue 2
> ************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190702/055d7255/attachment-0001.html>


More information about the Flask mailing list