Introduction
Django is a high-level Python web framework for developing dynamic websites and applications. Django that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of Web development.
Install Global Packages
If you wish to install Django using the Ubuntu repositories, use this process.
$ sudo apt update
Next, check which version of Python you have installed.
$ python3 -VOutput$ Python 3.6.5
Next, install Django:
$ sudo apt install python3-django
Check the installation by typing:
$ django-admin --versionOutput3.2
Refresh the local packages
$ sudo apt update
Check the version of Python you have installed:
$ python3 -VOutputPython 3.9
Next, let’s install pip
from the Ubuntu repositories:
$ sudo apt install python3-pip
Once pip
is installed, you can use it to install the venv
package:
$ sudo apt install python3-venv
Whenever you start a new project, you can create a virtual environment for it, first start by creating and moving into a new project directory:
$ mkdir ~/mynew_project$ cd ~/mynew_project
Next, create a virtual environment within the project directory using the python
command that’s compatible with your version of Python. We will create our virtual environment my_env
, but you should give it a descriptive name when creating your own project.
Type this:
$ python3 -m venv my_env
This will install standalone versions of Python and pip
into an isolated directory structure within your project directory. A directory will be created with the name you select, which will hold the file hierarchy where your packages will be installed.
To install packages into the environment, you must activate it by typing:
$ source my_env/bin/activate
Your terminal should change to reflect that you are now in your virtual environment. It will look something like this:
(my_env)username@hostname:~/mynew_project$.
In your new environment, you can use pip
to install Django. Regardless of your Python version, pip
should just be called pip
when you are in your virtual environment. Also, you do not need to use sudo
since you are installing the packages locally:
(my_env) $ pip install Django
To leave your virtual environment, you need to type in the deactivate
command on the system:
deactivate
Your prompt should revert to the conventional display. When you wish to work on your project again, re-activate your virtual environment by moving back into your project directory and activating:
cd ~/mynew_projectsource my_env/bin/activate
Create a Sample Project
With Django installed, you can begin building your project. We will go over how to create a project.
First, create a directory for your project and change into it:
mkdir ~/myfirstdjangoproject
cd ~/myfirstdjangoproject
Next, create your virtual environment:
python3 -m venv my_env
Activate the environment:
source my_env/bin/activate
Install Django:
pip install Django
To build your project, you can use django-admin
with the startproject
command. We will call our project myfirstdjangoproject
, this will create a directory within your current working directory that includes:
- A management script,
manage.py
, which you can use to administer various Django-specific tasks.
To avoid having too many nested directories, however, let’s tell Django to place the management script and inner directory in the current directory
**notice the dot at the end** :
django-admin startproject myfirstdjangoproject .
To build your app, you can use python manage.py
with the startapp
command. We will call our app myfirstapp
:
python manage.py startapp myfirstapp
Now have fun building your application. GOOD LUCK . 😄