Set Up a new Django Project on Ubuntu 20.04

Set Up a new Django Project on Ubuntu 20.04

  • Django
  • 585 Views

This tutorial focuses on initial steps to start a new django web application

  By the end of this tutorial, you’ll know how to:

  • Install python

  • Install virtual environment

  • Install python package manager (pip)

  • Set up a virtual environment

  • Install Django

  • Pin your project dependencies

  • Set up a Django project

  • Start a Django app

 

1. Update the packages list
sudo apt update 

 

2. Install Supporting Software
sudo apt install software-properties-common

 

3. Add the deadsnakes PPA to your system’s sources list, 
sudo add-apt-repository ppa:deadsnakes/ppa

 When prompted, press [Enter] to continue.

 then refresh the package List again

sudo apt update

 

4. Install python 3.8
sudo apt install python3.8

Verify the installation by typing

python3 --version

 

5 Install development tools to ensure that we have a robust setup for our programming environment:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

 

6 Install pip  (package installer for Python)
sudo apt install -y python3-pip

 

7. Install python virtual environment (venv)
sudo apt install -y python3-venv

  

8. Create a new Django project
  • Create a folder to contain your project, here we will call the project "my site", create the folder and navigate into the folder 

mkdir mysite

cd mysite

 

  • Setup a new virtual environment, lets call it "venv"

python3 -m venv venv

 

  • Activate the new virtual environment, 

after the virtual environment has been activated, your terminal will be appended by (virtual environment) name, for this case it will be (venv)

source venv/bin/activate

 

(venv) sammy@ubuntu:~/mysite$

 

  • Install django into the dedicated workspace
python -m pip install django

Once installed, verify your Django installation by running a version check:

(venv) sammy@ubuntu:~/mysite$  django-admin --version

 

 

  • Pin your dependencies,

django allows you to pin your dependencies to keep track of the package versions you have installed

python -m pip freeze > requirements.txt

 

  • Create new project, lets create a new project called mysite, and place it in the current directory

django-admin startproject mysite .

 

The resulting project structure will look as below

 

 

9. Run the created django project

python manage.py runserver 

 

by default django will run on localhost on port 8000, to specify a different port (i.e 5000) run the following command 

 

python manage.py runserver 5000

 

and the resulting web page will be as follows