Search code examples
python-3.xflaskpython-decoratorsfunctools

Python Decorators, Flask, Args


I am wondering why when I go to the route '/al6759@school.edu/info', the console prints

()
al6759@school.edu

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')

Solution

  • 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:  al6759@school.edu
    kwargs:  {'email': 'al6759@school.edu'}
    al6759@school.edu