Skip to content

Introduction to Views

Resources:
- Section 6 Code Files

Let us discuss what is a view.

A view function is a Python function that takes a web request from the client and returns a web response to the client.

For example: if you browse a website in your browser, first of all, you will send a web request to the web server and once your request is processed, you get a web response in the form of HTML, audio, video, or any other format.

Therefore, in django, a view is something that takes a web request, processes it, and then provides a response to the client either in HTML or any other format.

Now, let us learn how to create a view in django that takes an HTTP request and returns an HTTP response.


  • First of all, you need to download the project code files from the resources section.

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Then, migrate the changes to the SQLite database.

python manage.py migrate
  • Also, create a new superuser account to log in to admin panel.
python manage.py createsuperuser

Now, in the mysite folder of your project root directory, create a new file named views.py

Now, to create a view in django, we need to create a function that takes a request in the parameter.

def index(request):
    pass

Now, to return an HTTP response, we need to import it from django.http

from django.http import HttpResponse

In the index function return the HTTPResponse which you have imported.

def index(request):
    return HttpResponse("Hi")

In this way, we have created a simple view that takes a web request and returns an HTTP response with a message "Hi".


Now, the problem is by just writing the view, we can't access this view in the browser.

We need to configure URL routing to route our request to the view that we have created.

So, first, open your setting.py file and find the ROOT_URLCONF configuration.

ROOT_URLCONF = 'mysite.urls'

Here, the root URL configuration is set to mysite.urls. That means whenever the django receives any web request, then django checks for URL routes in mysite.urls.

In the mysite folder there is a file named urls.py file. Open it.

In the mysite.urls you can find urlpatterns.

urlpatterns = [
    path('admin/', admin.site.urls),
]

As you can see in the urlpatterns, one path is created by django for the admin panel. In that path, all the requests that has "/admin" in the URL name is routed to admin.site.urls.

That means if you open the browser and search for the 127.0.0.1:8000/admin then that request is routed to admin.site.urls.

Now, let us create new path for the view which we have created.

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path('admin/', admin.site.urls),
]

By adding this path, we are telling django that whenever someone sends a request to the URL that has an empty string, that means the domain name, then django should route that request to the index function of views.py file.

The name argument is used to give this pattern a unique name, which can be used for reverse URL matching (i.e., generating URLs from patterns).

So, in the browser, if you search for 127.0.0.1:8000/ then django will route this request to the index view that we have created.

Therefore, when you visit the 127.0.0.1:8000/ you will get a Hi message. Now, let us revise what we have learned so far.

  • First of all, we send a request to 127.0.0.1:8000/ address.
  • Django receives this request and checks the URL configuration in the settings.py file.
  • In the settings.py file, we have mentioned the location of URL Configuration file i.e. mysite.urls.
  • Django goes to the mysite.urls and checks for path in the urlpatterns.
  • In the first path, we have configured that when the url is empty or when it is the domain name, then route that request to the index view defined in the views.py file.
  • In the views.py file, django searches for index view.
  • In the index view, we are taking that request as a parameter and then returning an HTTPResponse "Hi".
  • Django sends back that HTTPResponse to the browser and then the browser renders Hi message over the screen.

In this way, we have learned how to create a view and manage URL routing for that view.