Django Views

Jas Spencer
3 min readMar 28, 2021

In a previous post, I wrote about a basic overview of the Django framework. Generating a project which encompasses apps. Needless to say, there is much more detail to go in to which brings us to this post. Views or view functions are take in a web request and return a response. Now that response can be a wide variety of things, generally it will show something to the user. View function, or simply shortened to views, are defined in the views.py file of the app directory of your project.

Django also has something called modules which can be imported into files in you project directory and include functions which make the flow of the project work easier. For examples of view functions I’m going to import the ‘HttpResponse’ module, as well as a popular module called ‘datetime’ which is used to, you guessed it, help with dates and time stamps. To use:

from django.http import HttpResponse
import datetime

Let’s create a method that will tell the user what day it currently is. You know, in case they forgot. In the views.py file, we’d define the function as such:

from django.http import HttpResponse
import datetime
def what_is_today(request):
today = datetime.date.today()
return HttpResponse "<h1>Today is {{ today }}!</h1>"

Let’s go over what is happening

Modules are imported at the top of the file

The function is defined(the name can be whatever you want, generally it should relate to whatever is being returned)

Views take in a HttpRequest object as its first parameter, noted above as ‘request’

We use the ‘today()” method from the ‘datetime’ module to get today’s date and then store it in a variable called ‘today’.

The function then directly returns some HTML and we inject the ‘today’ variable using the double curly braces ‘{{}}’ to display the variable.

When it comes to importing modules, there are more direct ways to pull off methods which means that we don’t have to chain multiple methods. Like so:

from django.http import HttpResponse
import datetime
from datetime import date
def what_is_today(request):
today = date.today()
return HttpResponse "<h1>Today is {{ today }}!</h1>"

This just helps import that date method directly so it doesn’t have to be chained on to datetime every single time we want to use it.

Regarding the response, it is very unlikely that views will directly return some HTML. It is much easier to return a template which is a file that can contain all the glorious HTML you want. It makes the views much cleaner and easier to read. A quick example of using view functions to return a template would be like this:

from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'pages/index.html')

While there is a different module being imported than in the previous example, logically it works the same way. We want the ‘render’ method to that we can display a template. Upon the request, the view will look for the pages directory and render the template called ‘index.html’

Understanding views are essential to building out Django project. Its serves almost as a roadmap to help the user get from one place to another, working in tandem with the requests the user makes. Views can generate templates, alerts, pretty much anything with the proper syntax.

Have fun!

--

--

Jas Spencer

Software Engineer in NYC with expertise in Ruby on Rails, JavaScript, and React