DEV Community

Cover image for Let's Learn Django #3 Writing our first view
Rachit Khurana
Rachit Khurana

Posted on • Edited on

Let's Learn Django #3 Writing our first view

By now, we have seen how to initialise our project and the overview of different files in a Django project.

Now, we will write out the first view/page.

Creating the First View - Hello World!

Let's start by creating a simple view that will return a simple Hello World text.

For that, we first need to tell Django to include our app (the app that we created before using the django-admin startapp command) in the project.

We will open the settings.py file. There would be a list named INSTALLED_APPS . We just need to add our app name to that file. This allows Django to recognize and include your app in the project.

Somewhat like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'quiz'
]
Enter fullscreen mode Exit fullscreen mode

Here, quiz is the name of the app I created before.

Now, we will create a function that will take the request as a parameter & return Hello World! or anything you like. Kindly note, that request is a required parameter as it will contain all the details of the HTTP request.

We will create this function in quiz/views.py file.

Since we want to return plain text in the HTTP response, we will import it here.

HttpResponse is used to return plain text in the HTTP response. django.shortcuts already provide us with many methods through which we can return data.

The file will look like this:

from django.shortcuts import render, HttpResponse

# Create your views here.
def hello(request):
    return HttpResponse("Hello World!")
Enter fullscreen mode Exit fullscreen mode

Now to map this function to the home URL, we will edit the urls.py file.

urls.py file already contains a list by the name of urlpatterns. We will add our routes to this. Since we want to display Hello World on our homepage, our path will just be an empty string. And to map it to that function, we will import quiz.views here.

Our views.py file should look something like this now:

from django.contrib import admin
from django.urls import path
import quiz.views

urlpatterns = [
    path('admin/', admin.site.urls),
    path("", quiz.views.hello, name="hello")
]
Enter fullscreen mode Exit fullscreen mode

The name parameter in the path is optional and used for naming the URL pattern, allowing you to refer to it by name in other parts of the code.

Awesome, we have made our first view. Now to test it, we will run it using the following command

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

This will start the Django server & will serve it to the local host.

And now, when we go to http://127.0.0.1:8000/ we will see Hello World! on that page 🎉

And there you have it! We have successfully created our first view in Django that displays 'Hello World!' when accessing the homepage. This simple example lays the foundation for building more complex views and functionalities in your Django project. Keep exploring and experimenting with Django to unlock its full potential!

In the next blog, we will explore more about how to use templates.

Top comments (0)