I understand that we can use reverse() in FBV and reverse_lazy() in CBV. I understand that we have to use reverse_lazy() in CBV as the urls are not loaded when the file is imported. Ref: Reverse_lazy and URL Loading?
What I don't understand is :
How are urls are loaded when we call reverse from the FBV?
As when we import the views at the top of the urls.py in django app, urlpatterns list is yet to be evaluated. How does reverse() for FBV work but not for CBV?
Does my question makes sense?
Consider these two ways of defining the success_url. The first is commented out, the second is the function:
@CoffeeBasedLifeform : you are right, class attributes are evaluated on import, I checked after reading your answer. So,
success_url
we have to usereverse_lazy()
.reverse()
.Now it is crystal clear.
Thanks CoffeeBasedLifeform :)
Class attributes are evaluated on import. The answer to when or exactly how that happens, resides within the depths of python's import system.