Create a Django Project

There are many excellent Django tutorials available. Most of them, though, are written for Linux. This one will be for installation and development on a Windows platform.

Initial Setup

  • Install Python
  • Create a Virtual Environment
  • Install Django
  • Project Setup
  • Database Setup
Install Python

This project will be built using Python3.  For instructions on how to do this, please go to python.org. Make sure, before you select “Install Now,” you check the box at the bottom of the dialog box, “Add Python to PATH.” Once the installation has completed, run the following from your Command Prompt. You should be ready to start your development once you receive similar output.

c:\>python --version
Python 3.7.9
Create a Virtual Environment
  •  Create a directory for your virtual environment (venv).  I have multiple projects, and I store all my venv files in one directory creatively called envs.
  • From command prompt, navigate to the directory you just created, and run the following with the name you choose for this environment. I like to name my venv the same as my project name, but that is not necessary. In this command, I am naming my new environment tutorial. 
c:\envs>python -m venv tutorial

A virtual environment has been created and named tutorial.  You can see its folder under envs.

Activate the virtual environment by entering this from the command prompt:

c:\envs>tutorial\Scripts\activate

You will know your virtual environment has been activated when its name appears in parenthesis in front of your prompt.

(tutorial) c:\envs>
Install Django

Inside the virtual environment resides the latest version of Python which has been previously installed, as well as the latest version of pip.  To install Django simply run the following:

(tutorial) c:\envs>pip install Django
Results: (something like this - depending on the current version)

Collecting Django
  Downloading Django-4.0.4-py3-none-any.whl (8.0 MB)
     ---------------------------------------- 8.0/8.0 MB 6.1 MB/s eta 0:00:00
Collecting tzdata
  Downloading tzdata-2022.1-py2.py3-none-any.whl (339 kB)
     ---------------------------------------- 339.5/339.5 KB 7.0 MB/s eta 0:00:00
Collecting asgiref<4,>=3.4.1
  Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
     ---------------------------------------- 42.3/42.3 KB ? eta 0:00:00
Installing collected packages: tzdata, sqlparse, asgiref, Django
Successfully installed Django-4.0.4 asgiref-3.5.0 sqlparse-0.4.2 tzdata-2022.
Project Setup

After creating and activating the virtual environment and installing Django, the next step is to create a Django project. We will create only one project, but later we will create several apps within this project.

  1. Navigate to where you want the project – a folder which is separate from the virtual environment. I start my projects from the c drive.
  2. From that folder, run the following command with the name of your project. This will not only create the project and its necessary components, but even the folder in which it all resides.
(tutorial) c:\envs>cd c:\
(tutorial) c:\envs>django-admin startproject tutorial

Results: this directory structure. We will be editing the highlighted files.

  • tutorial
    • tutorial
      • _pycache_
      • _init_.py
      • asgi.py
      • settings.py
      • urls.py
      • wsgi.py
    • manage.py
    • db.sqlite3

Summary of these files created for the project:

  • __init__.py → A blank Python script which indicates to the Python interpreter that this directory is actually a Python package. It allows Python packages to be imported from the directories.
  • asgi.py → Provides a standard interface between async-capable Python web servers, frameworks, and applications.
  • settings.py → A file that contains the configuration of the Django project. We will be editing this file as we add new apps and make other project-wide changes.
  • urls.py → A file that contains URL patterns for the Django project. We will be editing this file in order to display html forms and run commands.
  • wsgi.py → A file that contains WSGI configuration properties of the Django project. It’s basically a Python script that is used to run the development server and to help deploying to a production environment.
  • manage.py → A command line utility and allows to interact with the Django project in many ways. We will be using this command throughout our project.

First, it may be confusing that there are two directories with the same name. Actually, the name of the main directory can be changed to whatever name is preferred. However, the second-level directory name is hardcoded in some of Django specific files; therefore, it is not recommended to change its name.

Database Setup

Before going any further you need to make a decision about how data is to be stored.  By default, a SqlLite database has already been created for your project.  To use any database other than SqlLite, a change must be made to the connection in settings.py.  However, there are other options.  I will typically use a MySQL database.

mySql Setup
  • Create your schema in your MySQL database.  I am naming mine tutorial. For details concerning this, go to MySQL.com.
  • Install mysqlclient. From the command prompt run:
(tutorial) c:\tutorial>pip install mysqlclient
  • Open tutorial\settings.py and change the DATABASES code to the block of code below.
  • Tip: This is Python code, and must follow Python syntax rules. For a little information about this, go to: Python Syntax
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tutorial',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
  • Some standard Django database tables should be created before proceeding with creating apps.  To do this, run the following from the command prompt:
(tutorial) c:\tutorial>python manage.py migrate

If you return to the database you created, in the tutorial schema, you should now see these tables that Django created:

  • auth_group
  • auth_group_permissions
  • auth_permission
  • auth_user
  • auth_user_groups
  • auth_user_user_permissions
  • django_admin_log
  • django_content_type
  • django_migrations
  • django_session
Run the project

Django comes with a built-in web server for development. To run the project on the default post, simply navigate where the manage.py file is located and run the command:

(tutorial) c:\tutorial>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 02, 2022 - 13:26:44
Django version 3.2.12, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Now, open your browser with this url: http://127.0.0.1:8000/ . It should open to a default Django welcome page.

Prepare to create project apps

So far, we have a project, but it doesn’t do anything just yet. We will need to add apps to the project. But first, we have one more step. We need to create a user for our project.

(tutorial) c:\tutorial>python manage.py createsuperuser
Username (leave blank to use 'default'): admin
Email address: user@gmail.com
Password:
Password (again):
Superuser created successfully.
See the Results

Open your browner with this url: http://127.0.0.1:8000/admin/ . Log in with the credentials created for your superuser and you should see the following page. Now, we’re getting somewhere! Next, we just need some apps to tell our project what to do.

This image has an empty alt attribute; its file name is image.png

Coding is Fun!

Proudly powered by WordPress

Leave a Reply

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