[Django]-When should I use a lazy function in Django

2👍

This is the sort of thing that is only needed per-specific-situation, as you’ve intuited yourself.

A common example is with Generic Class-based Views. The attributes of the view are evaluated when the views.py file is read. But at the time it’s being read, the urls file will not have been read yet! So assigning to an attribute with reverse() will fail, because any url named in the arguments sent to reverse() will not be available to the view file. But with reverse_lazy(), you can assign a url to an attribute in the class-based view, and it will not be updated until the named url is actually available:

class RegisterView(CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('index')   # reverse() would fail here!
    template_name = 'registration/register.html')
    

Again: success_url is an attribute of the view, and will be evaluated when the views.py file is read. But at that time, the urls will not have been read, so ‘index’, a named url, will not be available yet. We use reverse_lazy() instead of reverse() to get around this problem; ‘index’ is not evaluated until it’s actually needed, and now we can define our attribute as desired.

Note that if your view has a user-defined method, you can use the normal reverse() call inside the method, if needed, because unlike view attributes, a view’s methods are not evaluated until the method is explicitly called — and by that time, the urls will be available.

There are other use cases, for sure, but CBV’s are a common case where reverse_lazy() becomes necessary, specifically when assigning urls to attributes.

Leave a comment