Let's Learn Django #3 Writing our first view

Full Stack Developer | Pythonista | Student | Programmer | Tech Enthusiast | Learner | Linux 🖤
Search for a command to run...

Full Stack Developer | Pythonista | Student | Programmer | Tech Enthusiast | Learner | Linux 🖤
No comments yet. Be the first to comment.
In this series, I will explain how you can start building awesome web apps with Django easily
We have already seen how to write our first view in the previous blogs. In this blog, we will explore how to use & render templates. What are templates? Templates are simply HTML files that we can render as a response to a URL/view.Django templates o...
The Beginning: Building My First "Real" App Recently, I embarked on building a Splitwise-like expense-splitting app using React Native and Expo. This wasn't just another side project—it was my first proper dive into this tech stack, with React Native...

As AI continues to evolve, one of the limitations of generative AI is its tendency to hallucinate—producing inaccurate or nonsensical outputs, especially when it lacks relevant context. This is where Retrieval-Augmented Generation (RAG) comes in. RAG...

The general perception is that deploying applications on the cloud is complicated, but that is not always true. We have really easy ways to deploy stuff on the cloud as well. So I will be demonstrating how to deploy Django Webpps to Azure easily. Pr...

Linux is known for its flexibility and powerful command-line tools. To make the most out of your Linux system, here are five productivity tools that can enhance your workflow. 1) Zoxide (z) So you might be wondering what exactly is zoxide ? According...

In the last blog, we discussed how can we use Django templating to the fullest. In this blog, we will discuss how can we use databases and handle our data in Django. Introduction to Django databases I am hoping everyone knows what a database is, but ...

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.
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'
]
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!")
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")
]
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
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.