• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

In Flask, set a cookie and then re-direct user

It seems like in Flask, cookies are set by modifying the response object directly.

How can I return a response object, but also redirect a user to a different page upon successful login? I'd like to specifically redirect the user instead of rendering a different page, in case the user hits REFRESH.

Here's my current code, which simply displays the same page, login.html:

@app.route('/login', methods=['POST', 'GET'])
def login():
  errors = []

  if request.method == 'POST':

    email = request.form['email']
    password = request.form['password']

    #Check the user's e-mail
      u = User(email)
    except UserError, e:

      #Check the user's password
      if not u.authenticatePassword(password):
        errors.append(('password','Invalid password'))
        return render_template('login.html',error=errors)

    #Set the session
    s = Session()
    s.user_id = u.user_id
    s.ip = request.remote_addr

    #Try to set the cookie
    if s.setSession():
      response = make_response( render_template('login.html',error=errors))
      response.set_cookie('session_id', s.session_id)
      return response

  return render_template('login.html',error=errors)

You should change your code to something like:

from flask import make_response
if s.setSession():
    response = make_response(redirect('/home'))
    response.set_cookie('session_id', s.session_id)
    return response
  • 55
Reply Report

Trending Tags