Django Views – A Beginners Guide

Django Views

In this tutorial, we are going to discuss more on the Django views and what types of views we can have for our webpage.

Also, we will get more information about what exactly we did in the views.py in our last article on Django URL mapping


What exactly is a view?

View function or simply a view can be thought of as a python function which, on taking up request returns a specific response to it. The response can be anything from a simple text to an HTML file to something like an image etc.

In a typical web application, we have views such as:

  1. Homepage View
  2. Detail/instruction View
  3. Help page View
  4. FAQ View

And so on.

Each view has a different URL endpoint that can be requested and it links to a particular view function in views.py

Now we will look into different types of views.

Views
Views

Function-based Views

In this type, Views are written as Python functions taking in request(HttpRequest) object as argument and returning a corresponding response(HttpResponse).

Django function-based views are used to make CRUD operations(Create, Retrieve, Update, and Delete). You will learn more about this later.

So all 4 will have different View – create view, Retrieve view, update view, delete view

Class-based Views

As the names suggest, the views are written as Classes instead of Functions to return the HTTP response to the particular requests. Hence in this, we can have all CRUD as methods thus having all of them in the same view.

Also, class-based Views are dived further into many types, few of which are important and we are going to learn in the upcoming articles.

  • Detail View
  • List View
  • Mixins

Now since we are dealing with responses and mainly HTTP responses, we should learn what they are.


HTTP Status Codes

These HTTP attributes are used to give more information to the client regarding the responses from the webserver. The most common and frequently used HTTP attributes that we need to learn are:

1. HTTP 200 – status OK

This means that the status is OK, which can be thought of as that everything was carried out correctly.

2. HTTP 201 – status created

This means that the status is created, we might use this one under Create/POST method.

3. HTTP 400 – Bad request

This means that the server will not process the request.

4. HTTP 404 – Not Found

This means that the server is unable to find the response that it needs to return


Addition of HTTP Attributes to Views

Now we will add the HTTP attribute in the response.  For that, we use the function called status.

In the function views, add the code:

return HttpResponse("Hello World",status = 200)
Http Attributes
HTTP Attributes

Now let’s look into the views.py and learn what we are doing there.


Views.py

We’ll understand the different types of views here.

1. View having text as response

Here as you can see we used function-based Views to write up the sayHello, Books, BookID View.

Here in the View function, we are making the request as the argument and then returning a HttpResponse(‘Hello World’).

So here basically, the response we are sending back is just the word Hello World.

Text Response
Text Response

And that’s it, we can now run the server and test the code.

Runserver
Runserver
Say Hello
Say Hello

Now let us send back a simple HTML line as a response.

2. View having HTML line and image as response

Here we have stored HTML code in a variable called HTML. And then we are sending it as a response back. In the HTML code, we are sending a heading and a picture named Lena.jpg

In the function sayHello add the code:

html = "<h1>Hello There This is a HTML file</h1><img src="lena.png" alt="picture" width = "104" height="142">
HTML Response
HTML Response

And that’s it, we can now run the server and test the code.

Runserver
Runserver
Html Response 1
Html Response 1

Therefore, we can see that the server is running perfectly.

Conclusion

And that brings us to the end of this article. Now, let’s move on to the next section. Stay tuned for more articles on Python Django and Flask in the coming few days!