Installation

In this tutorial, we’ll learn how to install Django, create a new Django project and view its directory structure.

I. Install Django

  • Firstly, we need to install pip
  • To install Django, run pip3 install Django
  • To check Django version, run python -m django --version
  • To check Django sub-commands, run django-admin
  • To update Django, run pip3 install --upgrade django

II. Create a new Django project

To create Django project, we follow these steps:

  • Run django-admin startproject PROJECT_NAME to create a number of starter files for our Django project.
  • Run cd PROJECT_NAME to navigate into your new project’s directory.

For example

$ django-admin startproject mysite
$ cd mysite

Note: You can’t name projects after built-in Python or Django components.

III. Directory structure

We can view the directory with the tree command or with Visual Studio Code.

Below I share how to install and use tree package.

1. For Mac OS

To install command line tools like the tree command on Mac, you can use the Homebrew package manager.

After installing Homebrew, run brew install tree (Y)

2. For Debian / Mint / Ubuntu Linux

$ sudo apt-get install tree

After installing tree, run tree to see the directory:

.
├── manage.py
└── mysite
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
  • manage.py file executes commands on our terminal. We won’t have to edit it, but we’ll use it often.
  • The inner mysite directory is the actual Python package for your project.
  • __init.py__ is an empty file that tells python that this is a Python package.
  • settings.py file contains important configuration settings for our new project.
  • urls.py contains directions for where users should be routed after navigating to a certain URL.
  • asgi.py is an entry-point for ASGI-compatible web servers to serve your project.
  • wsgi.py is how Python and webserver communicate.

IV. Start a development server

We can start a development server by running python manage.py runserver.

We’ll see this.

Django version 3.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

1. Explanation

127.0.0.1 is localhost. This development server is being run locally on your machine, meaning other people cannot access your website.

This is a running webserver so we have to leave it running while you’re viewing your site in the browser or else you won’t be able to see it.

To view the site, we access http://127.0.0.1:8000/ or http://localhost:8000/

You’ll see a default site that Django has created for us and we’ll modify this.

To stop the server, type ctrl C.

If you have any trouble with the website not reloading for any reason, then stop the server and run the server again.

2. Change the port

By default, the development server starts at port 8000.

To change the server’s port, we can pass it as a command-line argument. For example, we starts the server on port 5000:

$ python manage.py runserver 5000

V. Django Model-View-Template

Django is based on Model-View-Template (MTV) structure. It has 3 parts

  • Model: The interface of your data. It is responsible for maintaining data.
  • View: The user interface — what they see when render a website.
  • Template: A template consists of static parts of the desired output.