Installation

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

Create a new Django project

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

For example

$ django-admin startproject mysite
$ cd mysite

Directory structure

We can view the directory with the tree command.

  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)

  1. 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.
  • __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.
  • wsgi.py is how Python and webserver communicate
  1. Alternatively, we can also view your project in Visual Studio Code.

Open a development server

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

We’ll see this

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

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.