Web Applications with Django. Working with Templates in Django | by Sadrach Pierre, Ph.D. | Towards Data Science

Django is a powerful python-based web framework that enables the development of database-driven web applications. Today, many technologies use the Django framework including Instagram, Spotify and Youtube. In this post, we will discuss how to build a simple video game reviews application in Django using html and css templates.
Let’s get started!
The first thing we need to do is open up a terminal and install Django:
pip install django
Next, we’ll create a Django project. Let’s create a project called ‘my_web_app’ with the following command:
django-admin startproject my_web_app
We can take a look at the structure of the files created using the directory listing command, tree, and we see:
tree
We see that upon creating a new project we’ve generated several files. We have a ‘my_web_app’ directory which contains a ‘manage.py’ file and an additional ‘my_web_app’ folder. The ‘manage.py’ file is a command line utility that allows us to do things like create applications, run our application on our server, and much more. The ‘__init__.py’ method tells python that our application is a python package. The ‘settings.py’ allows us to modify settings and configurations. The ‘urls.py’ file contains the paths we will use to direct users. The ‘wsgi.py’ file allows our application to communicate with our server.
Now let’s proceed by creating a reviews application
python manage.py startapp reviews
And displaying the additional files:
tree
Now, let’s go into our reviews directory and open the ‘views.py’ file. We should see the following:
from django.shortcuts import render# Create your views here.
Let’s add the following code:
from django.shortcuts import renderposts = [{'author': 'Sadrach Pierre','title': 'Call of Duty','review': 'This game is just ok.','date_posted': 'March 23, 2020',}{'author': 'Sadrach Pierre','title': 'Call of Duty','review': 'This video game is amazing!','date_posted': 'March 23, 2020',}]def home(request):    context = {        'posts':posts    }    return render(request, 'reviews/home.html', context)
The ‘views.py ‘now contains posts with reviews for the video game Call of Duty. It also contains a ‘home’ function that will allow us to render the html code we will use to display the text visible to our users. Notice in the home function we are rendering the html code in the ‘reviews/home.html’ path.
To proceed, we need to create a template directory in the reviews directory and cd into the templates directory:
mkdir templatescd templates
Now, we need to create an additional reviews folder and cd into the reviews directory:
mkdir reviewscd reviews
Next we will create a ‘base.html’ file and a ‘home.html’ file in our ‘reviews/templates/reviews’ directory. Let’s create our base file:
vi base.html
And add the code found here. Next create our home html file:
vi home.html
and add the code found here.
Next we need to add a css file in our ‘my_web_app/reviews’ directory that will add additional visual elements to our page. Let’s move up two directories:
cd ../../
Let’s create a directory called ‘static’ and cd into our static directory:
mkdir staticcd static
Create an additional reviews directory in our static directory:
mkdir reviewscd reviews
create a ‘main.css’ file:
vi main.css
And add the code found here.
Now, let’s go to our root project directory and open ‘urls.py’, we should see:
from django.contrib import adminfrom django.urls import pathurlpatterns = [    path('admin/', admin.site.urls),]
let’s add the following path to our url list:
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [    path('admin/', admin.site.urls),    path('', include('reviews.urls')),]
Next let’s go into the ‘my_web_app/reviews’ directory and create a ‘urls.py’ file:
vi url.py 
And let’s add the following:
from django.urls import pathfrom . import viewsurlpatterns = [    path('', views.home, name='reviews-home'),]
Our final tree should look as follows:
The last thing we need to do is modify the ‘setting.py’ file in our ‘my_web_app’ directory:
In ‘setting.py’ we should see a list of installed applications:
INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',]
Let’s add our ‘reviews’ app to our list:
INSTALLED_APPS = [    'reviews.apps.ReviewsConfig',    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',]
We should be all set. Let’s navigate to our root project directory and run our app:
python manage.py runserver
We should see:
Let’s copy and paste ‘http://127.0.0.1:8000/’ in our browser and we should see:
This isn’t much so far. We can modify our posts in our ‘views.py’ file to include more interesting reviews. Let’s add a review for Call of Duty: Modern Warfare. I’ll take a quote from a review by The Guardian. I’ll also add a review for another 2019 game, Star Wars Jedi: Fallen Order, from IGN:
from django.shortcuts import renderposts = [{'author': 'The Gaurdian','title': 'Call of Duty: Modern Warfare','review': 'Call of Duty is perhaps the most divisive mainstream gaming brand of all time; a gung-ho, partisan blockbuster combat romp selling us a vision of rough and ready spec-ops superstars travelling the globe with their guns and their competence, helping freedom fighters while killing rogue paramilitary groups, without pausing too long to consider the differences between them.','date_posted': 'March 23, 2019',}{'author': 'IGN','title': 'Star Wars Jedi: Fallen Order','review': 'Jedi: Fallen Order pushes all the right buttons for a Star Wars action-adventure. It’s a genre remix that samples the combat and exploration of a lightened-up Dark Souls and the action and energy of Uncharted, and that works out to be a great fit for the return of the playable Jedi.','date_posted': 'March 21, 2019',}]def home(request):    context = {        'posts':posts    }    return render(request, 'reviews/home.html', context)
Now our reviews app looks like:
I’ll stop here but feel free to play around with the code yourself.
To summarize, in this post we discussed how to create a video game reviews web application using the Django web framework. You have the freedom to further build out this application as a personal video game blog or an online application for users to sign up and review video games. If you are interested in learning more about Django, I recommend Corey Schafer’s YouTube tutorials which can be found here. The code from this post can be found on GitHub. Thank you for reading!