#๐Ÿ”’ Flask Decorator problem

9 messages ยท Page 1 of 1 (latest)

sacred smelt
#

I am trying to construct a decorator ("admin_only") for (def add_new_post():) function.
This is my code https://paste.pythondiscord.com/QQPQ and i know that there is smth wrong with the decorator, probably i have to get some information about what kind of object flask returns when nobody is logged in. But the question is, why tf is this error occurring when i am tryin to reach home ("/") or other route after i am logging in as user 1?
"werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'add_new_post'. Did you mean 'delete_post' instead? "
How is this even related? I am not trying to reach 'add_new_post'. I even cant cause this error occurs.

clear agateBOT
#

@sacred smelt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

full urchin
#

!d functools.wraps - decorate the wrapper, otherwise all info about the original function (its name, signature with hints, etc) are lost

clear agateBOT
#

@functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)```
This is a convenience function for invoking [`update_wrapper()`](https://docs.python.org/3/library/functools.html#functools.update_wrapper) as a function decorator when defining a wrapper function. It is equivalent to `partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)`. For example...
bright mist
#

Basically ```py
@functools.wraps(func)
def wrapper(*args, **kwargs):

full urchin
#

!e managed to make a visual example of no wraps vs with wraps

from functools import wraps

def bad_deco(fun):
    def wrapper(*args, **kwargs): 
        # some stuff here maybe
        return fun(*args, **kwargs)
    return wrapper

def good_deco(fun):
    @wraps(fun)
    def wrapper(*args, **kwargs): 
        # some stuff here maybe
        return fun(*args, **kwargs)
    return wrapper

@bad_deco
def my_function(): print("whatever")

@good_deco
def my_function2(): print("whatever")

print(my_function) # lost the name, shows wrapper
print(my_function2) # has the original name
clear agateBOT
clear agateBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.