Django Views
To recall, a view in Django is a piece of Python code that allows a request to be taken in, performs an action based on the request, and then returns a response to the user, and hence forms an important part of our Django applications.
Inside Django, we have the option of building our views by following two different methodologies, one of which we have already seen in the preceding examples and is known as function-based views, while the other one, which we will be covering soon, is known as class-based views:
- Function-Based Views (FBVs): FBVs inside Django are nothing more than generic Python functions that are supposed to take an
HTTPRequest
type object as their first positional parameter and return anHTTPResponse
type object, which corresponds to the action the view wants to perform once the request is processed by it. In the preceding exercise,index()
andgreeting_view()
were examples of FBVs. - Class-Based Views (CBVs): CBVs are views that closely adhere...