-2

During login form validation, I query for a user and store the object on the form instance. I want to pass the user object into the template. Following Python, Flask - Passing Arguments Into redirect(url_for()) , I tried:

def home():
    form = LoginForm(request.form)
    # Handle logging in
    if request.method == 'POST':
        if form.validate_on_submit():
            login_user(form.user)
            flash("You are logged in.", 'success')
            redirect_url = request.args.get("next") or url_for("user.profile")
            return redirect(redirect_url, userobj=form.user)

I'm redirecting to :

@blueprint.route("/profile")
@login_required
def profile():
    return render_extensions("users/profile.html")

and again I want to pass the user object into profile.html template.

I'm getting:

TypeError: redirect() got an unexpected keyword argument 'userobj'

How can I fix this?

Community
  • 1
  • 1
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    You don't seem to have followed the advice in that linked question at all. For a start, the parameter is to `url_for`, not `redirect`; and secondly, the answer explicitly points out that your target handler actually has to accept a parameter, which yours doesn't. – Daniel Roseman Mar 26 '16 at 10:20
  • Thanks for pointing that out. In my case I realized I had access to flask-login's current_user, which I ended up using – user1592380 Mar 26 '16 at 15:24

1 Answers1

1

You may not be doing it correct. user which is logged in is available through current_user which is available in from flask.ext.login import current_user

this is how i did

@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)
        return redirect(request.args.get('next') or url_for('main.index'))
    flash('Invalid username or password')
    return render_template('auth/login.html', form=form)

in the index view i am able to access it like current_user.username same in the template

try this it may help

peace
Community
  • 1
  • 1