Flask setting cookies












0















I try to set cookies in Flask, but I don't get what I want to. Instead of getting username I get an respone attached to my URL.
My routes.py



@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)

userCookie = request.form['username']
resp = make_response(render_template('index.html'))
resp.set_cookie('user', userCookie)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index', resp=resp)
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)


And I want to display the content of cooki in index.html



{% for r in resp %}
{{ r }}
{% endfor %}


Instead I get:



index?resp<Response+1250+bytes+[200+OK]>


What am I doing wrong?



[EDIT - logout method]



That's my method before adding cookies



@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))


So if I added cookies:



@app.route('/logout')
def logout():
resp = make_response(redirect('/login'))
resp.delete_cookie('user')


And if I rester server, login, the cookie is created, but after logout I can even go to the endpoint /login
return resp










share|improve this question





























    0















    I try to set cookies in Flask, but I don't get what I want to. Instead of getting username I get an respone attached to my URL.
    My routes.py



    @app.route('/login', methods=['GET', 'POST'])
    def login():
    if current_user.is_authenticated:
    return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
    user = User.query.filter_by(username=form.username.data).first()
    if user is None or not user.check_password(form.password.data):
    flash('Invalid username or password')
    return redirect(url_for('login'))
    login_user(user, remember=form.remember_me.data)

    userCookie = request.form['username']
    resp = make_response(render_template('index.html'))
    resp.set_cookie('user', userCookie)
    next_page = request.args.get('next')
    if not next_page or url_parse(next_page).netloc != '':
    next_page = url_for('index', resp=resp)
    return redirect(next_page)
    return render_template('login.html', title='Sign In', form=form)


    And I want to display the content of cooki in index.html



    {% for r in resp %}
    {{ r }}
    {% endfor %}


    Instead I get:



    index?resp<Response+1250+bytes+[200+OK]>


    What am I doing wrong?



    [EDIT - logout method]



    That's my method before adding cookies



    @app.route('/logout')
    def logout():
    logout_user()
    return redirect(url_for('index'))


    So if I added cookies:



    @app.route('/logout')
    def logout():
    resp = make_response(redirect('/login'))
    resp.delete_cookie('user')


    And if I rester server, login, the cookie is created, but after logout I can even go to the endpoint /login
    return resp










    share|improve this question



























      0












      0








      0








      I try to set cookies in Flask, but I don't get what I want to. Instead of getting username I get an respone attached to my URL.
      My routes.py



      @app.route('/login', methods=['GET', 'POST'])
      def login():
      if current_user.is_authenticated:
      return redirect(url_for('index'))
      form = LoginForm()
      if form.validate_on_submit():
      user = User.query.filter_by(username=form.username.data).first()
      if user is None or not user.check_password(form.password.data):
      flash('Invalid username or password')
      return redirect(url_for('login'))
      login_user(user, remember=form.remember_me.data)

      userCookie = request.form['username']
      resp = make_response(render_template('index.html'))
      resp.set_cookie('user', userCookie)
      next_page = request.args.get('next')
      if not next_page or url_parse(next_page).netloc != '':
      next_page = url_for('index', resp=resp)
      return redirect(next_page)
      return render_template('login.html', title='Sign In', form=form)


      And I want to display the content of cooki in index.html



      {% for r in resp %}
      {{ r }}
      {% endfor %}


      Instead I get:



      index?resp<Response+1250+bytes+[200+OK]>


      What am I doing wrong?



      [EDIT - logout method]



      That's my method before adding cookies



      @app.route('/logout')
      def logout():
      logout_user()
      return redirect(url_for('index'))


      So if I added cookies:



      @app.route('/logout')
      def logout():
      resp = make_response(redirect('/login'))
      resp.delete_cookie('user')


      And if I rester server, login, the cookie is created, but after logout I can even go to the endpoint /login
      return resp










      share|improve this question
















      I try to set cookies in Flask, but I don't get what I want to. Instead of getting username I get an respone attached to my URL.
      My routes.py



      @app.route('/login', methods=['GET', 'POST'])
      def login():
      if current_user.is_authenticated:
      return redirect(url_for('index'))
      form = LoginForm()
      if form.validate_on_submit():
      user = User.query.filter_by(username=form.username.data).first()
      if user is None or not user.check_password(form.password.data):
      flash('Invalid username or password')
      return redirect(url_for('login'))
      login_user(user, remember=form.remember_me.data)

      userCookie = request.form['username']
      resp = make_response(render_template('index.html'))
      resp.set_cookie('user', userCookie)
      next_page = request.args.get('next')
      if not next_page or url_parse(next_page).netloc != '':
      next_page = url_for('index', resp=resp)
      return redirect(next_page)
      return render_template('login.html', title='Sign In', form=form)


      And I want to display the content of cooki in index.html



      {% for r in resp %}
      {{ r }}
      {% endfor %}


      Instead I get:



      index?resp<Response+1250+bytes+[200+OK]>


      What am I doing wrong?



      [EDIT - logout method]



      That's my method before adding cookies



      @app.route('/logout')
      def logout():
      logout_user()
      return redirect(url_for('index'))


      So if I added cookies:



      @app.route('/logout')
      def logout():
      resp = make_response(redirect('/login'))
      resp.delete_cookie('user')


      And if I rester server, login, the cookie is created, but after logout I can even go to the endpoint /login
      return resp







      python cookies flask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 14:44







      Frendom

















      asked Nov 21 '18 at 23:35









      FrendomFrendom

      386




      386
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Cookies are set in one request and can be used in another request.



          To overcome this, use redirect in make_response.



          I have attached an example of login/logout functionalities using cookies:



          app.py:



          from flask import Flask, render_template, request, make_response, flash, redirect

          app = Flask(__name__)
          app.config['SECRET_KEY'] = 'SUPER SECRET'

          @app.route('/', methods = ['GET'])
          def home():
          username = request.cookies.get('username')
          if username:
          return render_template('home.html', username=username)
          return render_template('home.html')

          @app.route('/login', methods = ['GET','POST'])
          def login():
          username = request.cookies.get('username')
          if username:
          return render_template('login.html', username=username)
          if request.method=='POST':
          username = request.form.get('username')
          password = request.form.get('password')
          if username=='admin' and password=='admin':
          flash("Successful login", "success")
          resp = make_response(redirect('/'))
          resp.set_cookie('username', username)
          return resp
          else:
          flash("Wrong username or password", "danger")
          return render_template('login.html')

          @app.route('/logout', methods = ['GET'])
          def logout():
          resp = make_response(redirect('/'))
          resp.delete_cookie('username')
          return resp
          app.run(debug=True)


          home.html:



          <html>
          <head>
          <title>Home</title>
          </head>
          <body>
          {% with messages = get_flashed_messages() %}
          {% if messages %}
          <ul class=flashes>
          {% for message in messages %}
          <li>{{ message }}</li>
          {% endfor %}
          </ul>
          {% endif %}
          {% endwith %}
          {% if username %}
          Welcome {{ username }}.
          <a href="{{ url_for('logout') }}">Click here</a> to logout.
          {% else %}
          You are not logged in.
          <a href="{{ url_for('login') }}">Click here</a> to login.
          {% endif %}
          </body>
          </html>


          login.html:



          <html>
          <head>
          <title>Login</title>
          </head>
          <body>
          {% with messages = get_flashed_messages() %}
          {% if messages %}
          <ul class=flashes>
          {% for message in messages %}
          <li>{{ message }}</li>
          {% endfor %}
          </ul>
          {% endif %}
          {% endwith %}

          {% if username %}
          You are already logged in as{{ username }}.
          <a href="{{ url_for('home') }}">Click here</a> to go to home.
          <a href="{{ url_for('logout') }}">Click here</a> to logout.
          {% else %}
          <form method="post" action="">
          <label for="username">Username</label>
          <input type="text" name="username" id="username"/>
          <br/>

          <label for="password">Password</label>
          <input type="password" name="password" id="password"/>
          <br/>

          <input type="submit" name="submit" id="submit" value="Login"/>
          </form>
          {% endif %}
          </body>
          </html>


          Screenshots:



          1. Before login (no cookie):



          before login



          2. Login (no cookie):



          login



          3. After login (received cookie):



          after login



          4. After Logout (no cookie):



          after logout






          share|improve this answer
























          • Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

            – Frendom
            Nov 22 '18 at 11:18











          • I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

            – arsho
            Nov 22 '18 at 13:08











          • I added edit. If you need more just write

            – Frendom
            Nov 22 '18 at 14:45











          • What happens if you add logout_user() at the first line in my logout method?

            – arsho
            Nov 22 '18 at 16:09











          • Ok, it works fine now ;)

            – Frendom
            Nov 22 '18 at 16:21



















          1














          You need to return resp directly, make_response Convert the return value from a view function to an instance of response_class.



          userCookie = request.form['username']
          resp = make_response(render_template('index.html'))
          resp.set_cookie('user', userCookie)
          next_page = request.args.get('next')
          if not next_page or url_parse(next_page).netloc != '':
          return resp


          In the html, get the cookie by javascript



          <label id="label_id1"></label>

          <script type="text/javascript">
          alert(document.cookie);
          document.getElementById('label_id1').innerhtml = document.cookie
          </script>





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421941%2fflask-setting-cookies%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Cookies are set in one request and can be used in another request.



            To overcome this, use redirect in make_response.



            I have attached an example of login/logout functionalities using cookies:



            app.py:



            from flask import Flask, render_template, request, make_response, flash, redirect

            app = Flask(__name__)
            app.config['SECRET_KEY'] = 'SUPER SECRET'

            @app.route('/', methods = ['GET'])
            def home():
            username = request.cookies.get('username')
            if username:
            return render_template('home.html', username=username)
            return render_template('home.html')

            @app.route('/login', methods = ['GET','POST'])
            def login():
            username = request.cookies.get('username')
            if username:
            return render_template('login.html', username=username)
            if request.method=='POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if username=='admin' and password=='admin':
            flash("Successful login", "success")
            resp = make_response(redirect('/'))
            resp.set_cookie('username', username)
            return resp
            else:
            flash("Wrong username or password", "danger")
            return render_template('login.html')

            @app.route('/logout', methods = ['GET'])
            def logout():
            resp = make_response(redirect('/'))
            resp.delete_cookie('username')
            return resp
            app.run(debug=True)


            home.html:



            <html>
            <head>
            <title>Home</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}
            {% if username %}
            Welcome {{ username }}.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            You are not logged in.
            <a href="{{ url_for('login') }}">Click here</a> to login.
            {% endif %}
            </body>
            </html>


            login.html:



            <html>
            <head>
            <title>Login</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}

            {% if username %}
            You are already logged in as{{ username }}.
            <a href="{{ url_for('home') }}">Click here</a> to go to home.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            <form method="post" action="">
            <label for="username">Username</label>
            <input type="text" name="username" id="username"/>
            <br/>

            <label for="password">Password</label>
            <input type="password" name="password" id="password"/>
            <br/>

            <input type="submit" name="submit" id="submit" value="Login"/>
            </form>
            {% endif %}
            </body>
            </html>


            Screenshots:



            1. Before login (no cookie):



            before login



            2. Login (no cookie):



            login



            3. After login (received cookie):



            after login



            4. After Logout (no cookie):



            after logout






            share|improve this answer
























            • Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

              – Frendom
              Nov 22 '18 at 11:18











            • I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

              – arsho
              Nov 22 '18 at 13:08











            • I added edit. If you need more just write

              – Frendom
              Nov 22 '18 at 14:45











            • What happens if you add logout_user() at the first line in my logout method?

              – arsho
              Nov 22 '18 at 16:09











            • Ok, it works fine now ;)

              – Frendom
              Nov 22 '18 at 16:21
















            1














            Cookies are set in one request and can be used in another request.



            To overcome this, use redirect in make_response.



            I have attached an example of login/logout functionalities using cookies:



            app.py:



            from flask import Flask, render_template, request, make_response, flash, redirect

            app = Flask(__name__)
            app.config['SECRET_KEY'] = 'SUPER SECRET'

            @app.route('/', methods = ['GET'])
            def home():
            username = request.cookies.get('username')
            if username:
            return render_template('home.html', username=username)
            return render_template('home.html')

            @app.route('/login', methods = ['GET','POST'])
            def login():
            username = request.cookies.get('username')
            if username:
            return render_template('login.html', username=username)
            if request.method=='POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if username=='admin' and password=='admin':
            flash("Successful login", "success")
            resp = make_response(redirect('/'))
            resp.set_cookie('username', username)
            return resp
            else:
            flash("Wrong username or password", "danger")
            return render_template('login.html')

            @app.route('/logout', methods = ['GET'])
            def logout():
            resp = make_response(redirect('/'))
            resp.delete_cookie('username')
            return resp
            app.run(debug=True)


            home.html:



            <html>
            <head>
            <title>Home</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}
            {% if username %}
            Welcome {{ username }}.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            You are not logged in.
            <a href="{{ url_for('login') }}">Click here</a> to login.
            {% endif %}
            </body>
            </html>


            login.html:



            <html>
            <head>
            <title>Login</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}

            {% if username %}
            You are already logged in as{{ username }}.
            <a href="{{ url_for('home') }}">Click here</a> to go to home.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            <form method="post" action="">
            <label for="username">Username</label>
            <input type="text" name="username" id="username"/>
            <br/>

            <label for="password">Password</label>
            <input type="password" name="password" id="password"/>
            <br/>

            <input type="submit" name="submit" id="submit" value="Login"/>
            </form>
            {% endif %}
            </body>
            </html>


            Screenshots:



            1. Before login (no cookie):



            before login



            2. Login (no cookie):



            login



            3. After login (received cookie):



            after login



            4. After Logout (no cookie):



            after logout






            share|improve this answer
























            • Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

              – Frendom
              Nov 22 '18 at 11:18











            • I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

              – arsho
              Nov 22 '18 at 13:08











            • I added edit. If you need more just write

              – Frendom
              Nov 22 '18 at 14:45











            • What happens if you add logout_user() at the first line in my logout method?

              – arsho
              Nov 22 '18 at 16:09











            • Ok, it works fine now ;)

              – Frendom
              Nov 22 '18 at 16:21














            1












            1








            1







            Cookies are set in one request and can be used in another request.



            To overcome this, use redirect in make_response.



            I have attached an example of login/logout functionalities using cookies:



            app.py:



            from flask import Flask, render_template, request, make_response, flash, redirect

            app = Flask(__name__)
            app.config['SECRET_KEY'] = 'SUPER SECRET'

            @app.route('/', methods = ['GET'])
            def home():
            username = request.cookies.get('username')
            if username:
            return render_template('home.html', username=username)
            return render_template('home.html')

            @app.route('/login', methods = ['GET','POST'])
            def login():
            username = request.cookies.get('username')
            if username:
            return render_template('login.html', username=username)
            if request.method=='POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if username=='admin' and password=='admin':
            flash("Successful login", "success")
            resp = make_response(redirect('/'))
            resp.set_cookie('username', username)
            return resp
            else:
            flash("Wrong username or password", "danger")
            return render_template('login.html')

            @app.route('/logout', methods = ['GET'])
            def logout():
            resp = make_response(redirect('/'))
            resp.delete_cookie('username')
            return resp
            app.run(debug=True)


            home.html:



            <html>
            <head>
            <title>Home</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}
            {% if username %}
            Welcome {{ username }}.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            You are not logged in.
            <a href="{{ url_for('login') }}">Click here</a> to login.
            {% endif %}
            </body>
            </html>


            login.html:



            <html>
            <head>
            <title>Login</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}

            {% if username %}
            You are already logged in as{{ username }}.
            <a href="{{ url_for('home') }}">Click here</a> to go to home.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            <form method="post" action="">
            <label for="username">Username</label>
            <input type="text" name="username" id="username"/>
            <br/>

            <label for="password">Password</label>
            <input type="password" name="password" id="password"/>
            <br/>

            <input type="submit" name="submit" id="submit" value="Login"/>
            </form>
            {% endif %}
            </body>
            </html>


            Screenshots:



            1. Before login (no cookie):



            before login



            2. Login (no cookie):



            login



            3. After login (received cookie):



            after login



            4. After Logout (no cookie):



            after logout






            share|improve this answer













            Cookies are set in one request and can be used in another request.



            To overcome this, use redirect in make_response.



            I have attached an example of login/logout functionalities using cookies:



            app.py:



            from flask import Flask, render_template, request, make_response, flash, redirect

            app = Flask(__name__)
            app.config['SECRET_KEY'] = 'SUPER SECRET'

            @app.route('/', methods = ['GET'])
            def home():
            username = request.cookies.get('username')
            if username:
            return render_template('home.html', username=username)
            return render_template('home.html')

            @app.route('/login', methods = ['GET','POST'])
            def login():
            username = request.cookies.get('username')
            if username:
            return render_template('login.html', username=username)
            if request.method=='POST':
            username = request.form.get('username')
            password = request.form.get('password')
            if username=='admin' and password=='admin':
            flash("Successful login", "success")
            resp = make_response(redirect('/'))
            resp.set_cookie('username', username)
            return resp
            else:
            flash("Wrong username or password", "danger")
            return render_template('login.html')

            @app.route('/logout', methods = ['GET'])
            def logout():
            resp = make_response(redirect('/'))
            resp.delete_cookie('username')
            return resp
            app.run(debug=True)


            home.html:



            <html>
            <head>
            <title>Home</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}
            {% if username %}
            Welcome {{ username }}.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            You are not logged in.
            <a href="{{ url_for('login') }}">Click here</a> to login.
            {% endif %}
            </body>
            </html>


            login.html:



            <html>
            <head>
            <title>Login</title>
            </head>
            <body>
            {% with messages = get_flashed_messages() %}
            {% if messages %}
            <ul class=flashes>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
            </ul>
            {% endif %}
            {% endwith %}

            {% if username %}
            You are already logged in as{{ username }}.
            <a href="{{ url_for('home') }}">Click here</a> to go to home.
            <a href="{{ url_for('logout') }}">Click here</a> to logout.
            {% else %}
            <form method="post" action="">
            <label for="username">Username</label>
            <input type="text" name="username" id="username"/>
            <br/>

            <label for="password">Password</label>
            <input type="password" name="password" id="password"/>
            <br/>

            <input type="submit" name="submit" id="submit" value="Login"/>
            </form>
            {% endif %}
            </body>
            </html>


            Screenshots:



            1. Before login (no cookie):



            before login



            2. Login (no cookie):



            login



            3. After login (received cookie):



            after login



            4. After Logout (no cookie):



            after logout







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 5:21









            arshoarsho

            3,44211631




            3,44211631













            • Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

              – Frendom
              Nov 22 '18 at 11:18











            • I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

              – arsho
              Nov 22 '18 at 13:08











            • I added edit. If you need more just write

              – Frendom
              Nov 22 '18 at 14:45











            • What happens if you add logout_user() at the first line in my logout method?

              – arsho
              Nov 22 '18 at 16:09











            • Ok, it works fine now ;)

              – Frendom
              Nov 22 '18 at 16:21



















            • Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

              – Frendom
              Nov 22 '18 at 11:18











            • I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

              – arsho
              Nov 22 '18 at 13:08











            • I added edit. If you need more just write

              – Frendom
              Nov 22 '18 at 14:45











            • What happens if you add logout_user() at the first line in my logout method?

              – arsho
              Nov 22 '18 at 16:09











            • Ok, it works fine now ;)

              – Frendom
              Nov 22 '18 at 16:21

















            Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

            – Frendom
            Nov 22 '18 at 11:18





            Ok, it works fine to some extend. If i login in to website, the cookie and session is created. The cookie has value of my user. But after logout, the cookie is removed and I can login in again. I can even display my login form

            – Frendom
            Nov 22 '18 at 11:18













            I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

            – arsho
            Nov 22 '18 at 13:08





            I assume the problem is now in logout functionality. Can you update the question including the logout snippet?

            – arsho
            Nov 22 '18 at 13:08













            I added edit. If you need more just write

            – Frendom
            Nov 22 '18 at 14:45





            I added edit. If you need more just write

            – Frendom
            Nov 22 '18 at 14:45













            What happens if you add logout_user() at the first line in my logout method?

            – arsho
            Nov 22 '18 at 16:09





            What happens if you add logout_user() at the first line in my logout method?

            – arsho
            Nov 22 '18 at 16:09













            Ok, it works fine now ;)

            – Frendom
            Nov 22 '18 at 16:21





            Ok, it works fine now ;)

            – Frendom
            Nov 22 '18 at 16:21













            1














            You need to return resp directly, make_response Convert the return value from a view function to an instance of response_class.



            userCookie = request.form['username']
            resp = make_response(render_template('index.html'))
            resp.set_cookie('user', userCookie)
            next_page = request.args.get('next')
            if not next_page or url_parse(next_page).netloc != '':
            return resp


            In the html, get the cookie by javascript



            <label id="label_id1"></label>

            <script type="text/javascript">
            alert(document.cookie);
            document.getElementById('label_id1').innerhtml = document.cookie
            </script>





            share|improve this answer




























              1














              You need to return resp directly, make_response Convert the return value from a view function to an instance of response_class.



              userCookie = request.form['username']
              resp = make_response(render_template('index.html'))
              resp.set_cookie('user', userCookie)
              next_page = request.args.get('next')
              if not next_page or url_parse(next_page).netloc != '':
              return resp


              In the html, get the cookie by javascript



              <label id="label_id1"></label>

              <script type="text/javascript">
              alert(document.cookie);
              document.getElementById('label_id1').innerhtml = document.cookie
              </script>





              share|improve this answer


























                1












                1








                1







                You need to return resp directly, make_response Convert the return value from a view function to an instance of response_class.



                userCookie = request.form['username']
                resp = make_response(render_template('index.html'))
                resp.set_cookie('user', userCookie)
                next_page = request.args.get('next')
                if not next_page or url_parse(next_page).netloc != '':
                return resp


                In the html, get the cookie by javascript



                <label id="label_id1"></label>

                <script type="text/javascript">
                alert(document.cookie);
                document.getElementById('label_id1').innerhtml = document.cookie
                </script>





                share|improve this answer













                You need to return resp directly, make_response Convert the return value from a view function to an instance of response_class.



                userCookie = request.form['username']
                resp = make_response(render_template('index.html'))
                resp.set_cookie('user', userCookie)
                next_page = request.args.get('next')
                if not next_page or url_parse(next_page).netloc != '':
                return resp


                In the html, get the cookie by javascript



                <label id="label_id1"></label>

                <script type="text/javascript">
                alert(document.cookie);
                document.getElementById('label_id1').innerhtml = document.cookie
                </script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 4:50









                tooTiredtooTired

                1697




                1697






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421941%2fflask-setting-cookies%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?