Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between reverse() and reverse_lazy() in Django

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 the urls loaded when we call reverse from the FBV? As when we import the views at the top of the urls.py in a Django app, urlpatterns list is yet to be evaluated. How does reverse() for FBV work but not for CBV?

like image 200
RyuCoder Avatar asked Feb 07 '18 17:02

RyuCoder


People also ask

What is the use of Reverse_lazy in Django?

reverse_lazy() A lazily evaluated version of reverse(). It is useful for when you need to use a URL reversal before your project's URLConf is loaded. Some common cases where this function is necessary are: providing a reversed URL as the url attribute of a generic class-based view.

What is reversing in Django?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. The redirect variable is the variable here which will have the reversed value. So the reversed url value will be placed here.

What is reverse redirect in Django?

It is called reverse because it is a reverse process of determining which view should be called for a given URL (which process is called resolving). Redirects are not specific to Django or any other web frameworks. Redirect means that for a given URL (or action), the user should be instructed to visit a specific URL.

What is HttpResponseRedirect in Django?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.


2 Answers

Consider these two ways of defining the success_url. The first is commented out, the second is the function:

class NewJobCBV(LoginRequiredMixin, CreateView):     template_name = 'company/job.html'     form_class = newJobForm     # success_url = reverse_lazy('newJob')      def get_success_url(self, **kwargs):         return reverse("newJob") 

@CoffeeBasedLifeform : you are right, class attributes are evaluated on import, I checked after reading your answer. So,

  1. If we are using success_url we have to use reverse_lazy().
  2. If we are reversing inside a function we can use reverse().

Now it is crystal clear.

Thanks CoffeeBasedLifeform :)

like image 122
RyuCoder Avatar answered Sep 19 '22 14:09

RyuCoder


#importme.py def a():     print("FUNCTION HELLO")  class B():     print("CLASS HELLO")        >>> import importme >>> CLASS HELLO 

Edit: The reason: The class creation process involves executing the body of the class.

The class body is executed (approximately) as exec(body, globals(), namespace). [...] Once the class namespace has been populated by executing the class body, the class object is created by calling metaclass(name, bases, namespace, **kwds).

https://docs.python.org/3/reference/datamodel.html?highlight=metaclass#executing-the-class-body



My original answer text. You can ignore it - I'm just leaving it in because mirek's comment was a direct response to it:

Class attributes are evaluated on import. The answer to when or exactly how that happens, resides within the depths of python's import system.

like image 38
CoffeeBasedLifeform Avatar answered Sep 17 '22 14:09

CoffeeBasedLifeform