Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain return super().get(request, *args, **kwargs)

Tags:

django

Hi I want to understand this line of code:

return super().get(request, *args, **kwargs)

This line is part of a method in class based view in views.py file , for example

class HomePage(TemplateView):
    template_name = "index.html"

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return HttpResponseRedirect(reverse("afterlogin"))
        return super().get(request, *args, **kwargs)

I know this method will check if the current user is authenticated and redirect to a different tempalte (afterlogin) in this case if a user is logged in , otherwise it will stay in index.html and then this function will execute this line return super().get(request, *args, **kwargs) anyway {I don't understand this line} .for example, if we were redirect to afterlogin page for example,then what this last line will do then?? .. this is the general idea that I understand but I want to understand the details of the code like:

If anyone can explain:

1- why I have these parameters: (self, request, *args, **kwargs) in get function?

2- when this method : def get(self, request, *args, **kwargs): will be triggered or called?

3- explain the last line in the get method: return super().get(request, *args, **kwargs) step by step and what is its purpose.

I will be grateful if anyone can explain in details because I am still learning.. Thanks a lot

like image 696
Salam Qais Avatar asked May 05 '18 17:05

Salam Qais


1 Answers

The super() function represents the parent class of the current one (in this case, this means TemplateView).

In your code, this means that get() will, as you noted, first check if the user is authenticated; if it is, then it will redirect to the afterlogin URL. If the user is not authenticated however, the function will not return, so it will go on and call the get() method of TemplateView (with the same arguments that have been passed to your get()), that will output whatever it would normally output if you hadn't inherited it.

To see what exactly that would be, you can navigate to TemplateView in the source code and read its get() method.

About your second question: when a user navigates to a URL you defined in your project, Django will check the method used for the request (if they're just viewing the page for the first time, this will be GET; another example is POST, which you probably heard before) and call the method of your view (HomePage) with the corresponding name, get(). If you also define a post() method, that will be called when the user requests the page with a POST method, for example by submitting a form with the method="post".

About your first question: request is an HTTP request object that is passed to the function to basically tell them what the user('s browser) has requested, what it wants to do. *args is a list of all other positional arguments that might have been added to the get() call. The same states for **kwargs, which is instead a dictionary of keyword arguments. These can very well be empty, but it's good that we pass them on to the upper level so that we don't loose any functionality that may be needed.

Just as an example, if you go to a url like mysite.com/page/search?=myquery, this would be a GET request, and it would include a keyword argument with key search and value myquery, so your **kwargs would look like: kwargs = {"search": "myquery"}. In the same way, the contents of a form submitted by POST would be available to your method to do what you need to do with them.

like image 129
theberzi Avatar answered Oct 26 '22 16:10

theberzi