Mike
Mike

Reputation: 103

Python Decorators, Flask, Args

I am wondering why when I go to the route '/[email protected]/info', the console prints

()
[email protected]

Why is args empty even though user_info is sent a value? I tried to provide the least amount of code necessary to solve the problem. Thanks in advance.

from flask import Flask, render_template, request, redirect, session
from functools import wraps


def security(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        print(args)
        return f(*args, **kwargs)
    return wrap


@app.route('/<email>/info', methods=['POST', 'GET'])
@security
def user_info(email):
    print(email)
    return render_template('user_info.html')

Upvotes: 0

Views: 66

Answers (1)

v25
v25

Reputation: 7656

I think you want to print kwargs['email'] in your decorator.

So this for example:

def security(f):
    @wraps(f)
    def wrap(*args, **kwargs):

        print('email from kwargs: ', kwargs['email'])
        print('kwargs: ', kwargs)

        return f(*args, **kwargs)
    return wrap

Will give the output:

email from kwargs:  [email protected]
kwargs:  {'email': '[email protected]'}
[email protected]

Upvotes: 2

Related Questions