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.
#๐ Flask Decorator problem
9 messages ยท Page 1 of 1 (latest)
@sacred smelt
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.
Closes after a period of inactivity, or when you send !close.
!d functools.wraps - decorate the wrapper, otherwise all info about the original function (its name, signature with hints, etc) are lost
@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...
Basically ```py
@functools.wraps(func)
def wrapper(*args, **kwargs):
!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
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <function bad_deco.<locals>.wrapper at 0x7f7f82437f60>
002 | <function my_function2 at 0x7f7f82430680>
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.