How To Make A Website With Python And Django

Django web project consists of applications, and each Django web project can contain multiple applications. This article will tell you how to create and add a Django application to a web project. All the operation is implemented in a virtual environment. Please read How To Install Python Django In Virtual Environment first if you do not know.

1. Example Overview.

  1. The working folder in this example is virtualenv.
  2. my_env is just one virtual environment folder that I created.
  3. The folder my_django_project is the Django web project folder, and it contains one project settings folder my_django_project and one application folder to_do_list.
  4. The most important files in the folder my_django_project / my_django_project  are ( contains the web project configuration settings data, such as template file directory) and ( contains the web project request URL path to each application’s mappings.).
  5. Next, we will tell you how to implement all this step by step.

2. Go Into Python Virtual Environment.

  1. CD into the working directory.
    $cd /Users/....../virtualenv
  2. Run source my_env/bin/activate to dive into the virtual environment.
    $ source my_env/bin/activate

3. Create Django Web Application.

  1. Run the below command in the Python virtual environment to create to_do_list application.
    (my_env) 192:my_django_project $ python startapp to_do_list
  2. After the above step, the folder to_do_list is created in the current folder, and the most important files has been added to the to_do_list folder are, and

4. Define Django Application Model.

4.1 Define App Model.

  1. The app’s file contains all the app model class definitions. A model class is mapped to a database table. And the class attribute map to the table’s column. So open the file in an editor and add the below code. For convenience, I use the eclipse PyDev plugin to edit the file, it is simple and efficient. Please read How To Run Python In Eclipse With PyDev to learn more.
    from django.db import models
    # Create your models here.
    # This class maps to a table to_do_list_ToDo, the table name start with the app name.
    class ToDo(models.Model):
        # Maps to to_do_things table column.
        to_do_things = models.CharField(max_length=1000)
        # Maps to create_date table column.
        create_date = models.DateTimeField(auto_now_add=True)
        # Maps to handle_date table column.
        handle_date = models.DateTimeField()
        def __str__(self):
            ''' Return this model string value. '''
            return self.to_do_things


4.2 Activate App Model.

After creating the app model class, you need to add the to_do_list app in my_django_project/my_django_project/ file to activate the to_do_list app and use the model.

  1. Open file.
  2. Add to_do_list app in INSTALLED_APPS section.
         # Below is my app.

5. Create Django Model Mapped Table In Project SQLite Database.

  1. CD into the Django web project folder virtualenv / my_django_project.

    $ cd my_django_project
  2.  Create table to_do_list_todo in my_django_project SQLite database file ( my_django_project / db.sqlite3 ) with following command.
    (my_env) 192:my_django_project $ python makemigrations to_do_list
    Migrations for 'to_do_list':
        - Create model ToDo
    (my_env) 192:my_django_project $ python migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions, to_do_list
    Running migrations:
      Applying to_do_list.0001_initial... OK
  3. Use SQLiteStudio to open the db.sqlite3 file, you can see the table has been created.  Please read How To Install SQLite3 On Mac to learn how to use SQLite Studio.

6. Register ToDo Model In Django Admin Site.

  1. Open the file virtualenv / my_django_project / to_do_list /, and add the below code to it.
    from django.contrib import admin
    # Import the model we defined in
    from to_do_list.models import ToDo
    # Register the ToDo model class to the admin site.
  2. Now open a browser and browse the admin URL http://localhost:8000/admin/, there add a new section TO_DO_LIST in the dashboard, you can manage your custom models in this section.django-admin-page-add-model-section
  3. Click the Add link in the TO_DO_LIST section to add a new ToDo object as below picture.
  4. When you click the Save button, the ToDo object has been added to the list, and the data is also inserted into the SQLite database table to_do_list_todo.

7. Mapping URLs.

When a user requests a url, Django will invoke a related python function predefined in application/ ( to_do_list/ )  file.  There are two files.

  1. Web project level ( my_django_project/my_django_project/ ). This file contains the requested url to the application file mappings, you can see them in a later chapter.
  2. Application-level ( my_django_project/to_do_list/ ), you should create this file by hand. This file contains url path to view function mappings in this application.

7.1 Create Django Application Url Mapping File.

Create a file in the to_do_list directory, and input the below code in it. The urlpatterns variable in the below code is a list of page url and python function mapping configuration.

from django.urls import path
# import views from local directory.
from . import views

urlpatterns = [

    # When user request home page http://localhost:8000/to_do, it will invoke the home function defined in
    path('', views.home, name='home'),

    # The first parameter is the url path.
    # The second parameter is the response function defined in file. 
    # The third parameter is the url name which will be used in html file url tag.
    # For example, in html code {% url 'to_do_list:to_do_list' %} will be mapped to url http://localhost:8000/to_do/list_all
    path('list_all', views.to_do_list, name='to_do_list'),
    # <id> is a placeholder that will be replaced with the real record id.
    path('show_detail/<id>/', views.to_do_detail, name='to_do_detail'),

7.2 Add Django App Urls Mapping File In Project Urls Mapping File.

To easily manage different Django app url mappings, we can create url mapping file for each Django app, and then add all those app url mapping files in project url mapping files.

So edit the my_django_project/my_django_project/ file with below content.

from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url

urlpatterns = [

    # When request http://localhost:8000/admin/, it will use url mappings defined in file

    # When request http://localhost:8000/to_do, it will use url mappings defined in file
    # The include function must contains a tuple and a namespace key value pair. 
    # The tuple first element is the app urls mapping file, the second element is the application name.
    # If do not provide the application name, it will throw Specifying a namespace in include() without providing an app_name is not supported error.
    url('^to_do/', include(('to_do_list.urls','to_do_list'), namespace='to_do_list'))

8. Define View Function.

Now open virtualenv/my_django_project/to_do_list/ file and input the below code.

from django.shortcuts import render
# Import ToDo model class defined in current file.
from .models import ToDo

import os

# Calculate django application execute directory path.
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

# This function will return and render the home page when url is http://localhost:8000/to_do/.
def home(request):
    # Get the index template file absolute path.
    index_file_path = PROJECT_PATH + '/pages/home.html'

    # Return the index file to client.
    return render(request, index_file_path)

# This function will display todos in a list page the request url is http://localhost:8000/to_do/list_all.
def to_do_list(request):
    # Get all todos model object order by handle_date column.
    todos = ToDo.objects.order_by('handle_date')

    # Add the todos list in Django context.
    context = {'todos' : todos}

    # Get the list page absolute path. 
    to_do_list_file_path = PROJECT_PATH + '/pages/list.html'

    # Render and return to the list page.
    return render(request, to_do_list_file_path, context)

# Display the todo detail information in web page. The input parameter is todo id. The request url is http://localhost:8000/to_do/show_detail/3/.
def to_do_detail(request, id):

    # Get todo object by id.
    todo = ToDo.objects.get(id=id)

    # Set the todo object in context.
    context = {'todo' : todo}

    # Get todo detail page absolute file path.
    to_do_detail_file_path = PROJECT_PATH + '/pages/detail.html'

    # Return the todo detail page.
    return render(request, to_do_detail_file_path, context)

9. Create Template Files.

In step 8, you can see some Html files are used in the python function. These Html files are called template files. There are four Html template files in this example. Before introducing them, let us look at a video demo about this example.

There are two navigation links on the example home page, one is the Home Page link, the other is the List To Dos link. When you click the List To Dos link, it will list all the todos links ( includes Eat at morning, Debug the source code, Running ) in the todos list page, when you click the Debug the source code link it will show the todo detail information page.

9.1 base.html

This is the parent template file, other files will inherit from this file. It contains the navigation link and a block content placeholder. So all this file’s child template files will contain navigation links.

<!-- The url tag's parameter format is app_name:url_mapping_name(defined in app's file.) -->
<a href="{% url 'to_do_list:home' %}">Home Page</a>
<a href="{% url 'to_do_list:to_do_list' %}" style="margin-left:10px">List To Dos</a>
{% block content %}{% endblock content%}

9.2 home.html

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Home Page</title>
<!-- extends the parent template file -->
{% extends "to_do_list/pages/base.html" %}
{% block content %}
This is the to do list web app home page.
{% endblock content%}

9.3 list.html

<!DOCTYPE html>
<meta charset="UTF-8">
<title>To Do List Page</title>
{% extends "to_do_list/pages/base.html" %}
{% block content %}
    <p>All Todos List</p>
        {% for todo in todos %}
            <li><a href="{% url 'to_do_list:to_do_detail' %}">{{todo}}</a></li>
        {% empty %}
            <li>No todos have been added yet.</li>
        {% endfor%}
{% endblock content%}

9.4 detail.html

<!DOCTYPE html>
<meta charset="UTF-8">
<title>To Do Detail Page</title>
{% extends "to_do_list/pages/base.html" %}
{% block content %}
        <li>Creation Date : {{todo.create_date}}</li>
        <li>Handle Date : {{todo.handle_date}}</li>
{% endblock content%}

9.5 Fix TemplateDoesNotExist Error.

When you browse the web page in above example, you may meet Template Does Not Exist error like below.


This means Django can not find the template file, because the template directory is not configured correctly in my_django_project/my_django_project/ file. Follow the below steps to fix it.

  1. Open file.
  2. Edit TEMPLATES / DIRS value to either the template file absolute path ( Django will find the template files in this path directly ) or an empty string like below '' ( In this case, you need to calculate the project path in the source code to get Html template file absolute directory like above section 8).
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            ''' DIRS can contains the absolute template file directory path. 'DIRS': ['/Users/zhaosong/Documents/WorkSpace/'], or the DIRS at least contains one empty string, otherwise the django system will throw TemplateDoesNotExist error. This is a bug of Django code.'''
            'DIRS': [''],
            'APP_DIRS': True,
                'OPTIONS': {
                    'context_processors': [
  3. Now start the Django web server and run the example as in the video.

10. Question & Answer.

10.1 How to save client response data into database table in Django application.

  1. I have developed a simple web application use Django, but it is very basic. What I need is to get the user response data and save the data to somewhere like a database. I do not know how to do it in the python Django application, can anyone tell me how to do it?
  2. Django provides built-in models to save client response data to a database table. You can define the Django model follow the instructions in this article, and each model can map to one table in your database, the example uses SQLite database, because Python supports the SQLite database by default, you do not need to install SQLite database python driver module. If you want to use other databases such as MySQL database, you need to install the MySQL database python module, you can read the article How To Connect MySQL Database In Django Project, Python Connect To MySQL Database Example.

10.2 Django TemplateDoesNotExist error occurred on different machines.

  1. I have two machines, they both installed Ubuntu, Nginx, and Python. The difference is the python version, one uses python 3.7 the other uses python 3.5. And when I request a template Html file in the two Django applications, one machine throws the error message TemplateDoesNotExist but the other does not. I do not know why, maybe there is something wrong with my machine one, but I can not fix it. I had specified the TEMPLATE_DIRS value in the file. And I am sure the template file exists on both machine and the directory is correct. Below is the file content.
    # Get the template file saved path, the template files path is the Django python file saved path.
    SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
        os.path.join(SETTINGS_PATH, 'templates'),

    And the above settings should find the template file in a path like /usr/lib/python3.5/site-packages/projectname/templates/appname1/template1.html.

  2. The TEMPLATE_DIRS settings in your Django project’s file will make Django find the template files in your-project-directory/templates folder. So your settings will find the template file in the path /usr/lib/python3.5/site-packages/projectname/templates/template1.html, so you need to move your template folder under the project folder, not the Django application folder.
  3. You can fix this kind of error by adding your Django app from the beginning. Because maybe there is something wrong with your configuration file during the development process. You can follow the below steps. 1. Create your Django app and template file directory under the Django project folder. 2. Add the Django app to the INSTALLED_APPS list value in the file. 3. Add the Django app’s templates directory in the file TEMPLATES list value.
            'DIRS': [os.path.join(BASE_DIR, 'templates'),
                     os.path.join(BASE_DIR, 'your_django_app_name', 'templates', 'your_django_app_name'),

2 thoughts on “How To Make A Website With Python And Django”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.