Django - Modifying Home Page

In last tutorial we started django project with default home page. Now we will see how can we modify home page and create our own with basic HTML. To do that we need to modify Views and URLs. But, for now we won't do any changes in our main view and url file, instead of this we will create an app and do changes in that. It’s time to create a Django app.

Django App?

It’s worth explaining the terminology here, because this tends to trip up beginners. We’d already created a project, in Chapter 1, so what’s the difference between a project and an app? The difference is that of configuration vs. code:

  • A project is an instance of a certain set of Django apps, plus the configuration for those apps. Technically, the only requirement of a project is that it supplies a settings file, which defines the database connection information, the list of installed apps, the TEMPLATE_DIRS, etc.

  • An app is a portable set of Django functionality, usually including models and views, that lives together in a single Python package.

For example, Django comes with a number of apps, such as a commenting system and an automatic admin interface. A key thing to note about these apps is that they’re portable and reusable across multiple projects.

We will divide our project into multiple apps to do a different kinds of tasks. e.g Stock Management, Invoices, Billing, etc. First we will create Stock Management system.

Creat django app

To create any django app run below command

python3.8 startapp stockmgmnt

Now this will create a folder with name stockmgmnt in your project folder. Including

Adding App in Project

Now that we have created our app we need to import that in our project. To do that browse to your file and look for INSTALLED_APP section. You will find multiple apps imported, these are default Django apps to manage, session, authentication, etc. To add our app just add your app name here as 'stockmgmnt'. It should look something like:




Now that our app is imported we need to create a base URL for our app. The base URL helps us to manage the URL application. With the help of the base URL, Django identifies which application it needs to call. For our project whenever we browse URL as "https://<domain-name>/stock/" it should go to our stock application. 

To do so we need to brose to file in your project folder (This file is not of your app folder). This file has default entry of admin URL. When you browse "http://<domain-name>/admin/" you can browse admin app and it will ask for username and password. We will see in a later chapter how can we manage this app. For now we need to add our stockmgmnt application URL. For that just add below line after admin entry:

path('stock/', include('stockmgmnt.urls')),

Now you URLPATTERN should look like:

urlpatterns = [
    path('stock/', include('stockmgmnt.urls')),

In that entry we configured Django to check the configuration of all http://<doman-name>/stock/ related URL in '' file of stockmgmnt folder. This file doesn't exist so we need to create one. Create file in stockmgmnt folder.

Creat file with below entries:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),



A URLconf is like a table of contents for your Django-powered Web site. Basically, it’s a mapping between URL patterns and the view functions that should be called for those URL patterns. It’s how you tell Django “For this URL, call this code, and for that URL, call that code.” In our file for '/' url we call code from file with home function.


We will create a home function in file so that when we call '/' URL of stock application we can get a response.

By default, your view file contains an import code. Just make sure it import HttpResponse, if not depending upon your Django version. you can import it your self with:

from django.shortcuts import render, HttpResponse

Create home function which can return text data:

def home(request):
    return HttpResponse("Welcome to Stock Management section")

In every function called by, we need to pass 'request' parameter. This parameter contains data including session, authentication data, POST data, etc. HttpResponse will return simple text data.


To verify run your project and browse

Return html Data

We can return pretty much all kinds of HTML data with HttpResponse. Just create another URL, to do that just add below line in your URLPATTEN


With this when you call /stock/home1/ you will get a response from function named home1 in file of stockmgmnt folder. 

Now let's create home1 function.

def home1(request):
    returndata = """
    <h1> This is second url </h1>
    return HttpResponse(returndata)


Now browse

Try creating new urls and test them. 


About the author



Please login to comment