Introduction :

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Django is a free and open-source web framework written in Python that adheres to the model template view (MTV) software architectural pattern. According to the Django Software Foundation, the model is the single definitive source of your data, the view describes the data that gets represented to the user via a Python callback function to a specific URL, and the template is how Django generates HTML dynamically.


in this tutorial, I will help you install Django on Linux, Windows, or Mac. After installing we will create and run our first Django application.

Step 1 – Install Python and pip

Linux and Mac:

Python is preinstalled in Linux. You may have to upgrade it. To install/update Python we have to update the local APT repository. Open your terminal window, we’ll input the command bellow. You can also ignore it.

#For Linux user 
$ sudo apt-get update && sudo apt-get -y upgrade

Django Software Foundation recommended that to use Python 3, we can install Python 3 by using the following command:

For Linux

$ sudo apt install python3

For Mac

$ brew install python3

To verify the installation enter the following command on your command prompt.

$ python3 -V

The output will look similar to this:

Output

Python 3.7.3

Now that we have successfully installed Python 3,we also need to install pip in order to install packages from PyPi, Python’s package repository.

For Linux

$ sudo apt install -y python3-pip

For Mac

$ sudo easy_install pip

To verify that pip is successfully installed, run the following command:

$ pip3 -V

The output will look similar to this:

Output

pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

For Windows

Go to python.org and download the official Python.exe installer and running it on your system.

After installation, open the command prompt and check the Python version by executing python --version. If you encounter a problem, make sure you have set the PATH variable correctly. You might need to adjust your PATH environment variable to include paths to the Python executable and additional scripts. For example, if your Python is installed in C:\Python34\, the following paths need to be added to PATH: source

If you don’t know how to read this article How To Install Python 3 On Windows 10

After installing python open your text editor like VScode or you can use command shell.

#to install Django
$ pip install django

After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.

To install pip read this article How To Install PIP To Manage Python Packages On Windows .

After installing pip we are ready to create our first Django project.

Follow How to Install Django on Windows

Step 2 — Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained development space, which isolates the installed software and packages from the rest of your machine’s global environment. This convenient isolation prevents conflicting packages or software from interacting with each other.

To install virtualenv, run the following command.

For Linux

$ pip3 install virtualenv

For Mac

$ sudo pip install virtualenv

Once it is installed, run the following command to verify the installation.

$ virtualenv --version

Output

virtualenv 20.0.31 from /home/pyprogramming/.local/lib/python3.8/site-packages/virtualenv/__init__.py

You have successfully installed virtualenv.

Step 3 — Install Django

First we have to create a new directory for our project. You can create manually or follow the command.

For Linux

$ mkdir MyBlog
$ cd MyBlog

For Mac

$ virtualenv MyBlog
$ cd MyBlog

MyBlog is a director, you can pick any name you want for you project.

While inside the MyBlog directory, let’s create our virtual environment and call it env.

For Linux

$ virtualenv env
# To activate the virtual environment
$ . env/bin/activate

For Mac

source bin/activate

You will know it’s activated once the prefix is changed to (env), which will look similar to the following depending on what directory you are in.

While in the environment, let’s install the Django package using pip. Installing Django allows us to create and run Django applications.

For Linux

$ pip install django

Once installed, verify your Django installation.

django-admin --version

For Mac

$ sudo pip install django==3.0.9

Once installed, verify your Django installation.

$ python -m django --version

Output

3.0.9

With Django installed, we can move on to creating a test project to make sure everything is working correctly.

Step 4 — Creating a Django Test Project

To test the Django installation, we will be creating a (blog) web application.

While in the MyBlog directory with active env, run the following command:

$ django-admin startproject MyBlog

Note: Running the django-admin startproject (projectname) {in my case its MyBlog} command will name both project directory and project package the (projectname) and create the project in the directory in which the command was run. If the optional (destination) parameter is provided, Django will use the provided destination directory as the project directory and create manage.py and the project package within it.

Now we can look to see what project files were just created. Navigate to the testsite (MyBlog) directory then list the contents of that directory to see what files were created:

$ cd MyBlog
$ ls

Output

manage.py  MyBlog

You will notice output that shows this directory contains a file named manage.py and a folder named MyBlog (testsite). The manage.py file is similar to django-admin and puts the project’s package on sys.path. This also sets the DJANGO_SETTINGS_MODULE environment variable to point to your project’s settings.py file.

Now navigate to the (testsite) MyBlog directory to view the other files that were created:

$ cd MyBlog
# to see the file list
$ ls

Output

Now navigate to the testsite directory to view the other files that were created:

Now we’ll need to add our server IP address to the list of ALLOWED_HOSTS in the settings.py file. Open settings.py with your favorite text editor like VScode. located in ~/MyBlog/Myblog/MyBlog/.

Add your server’s IP address inside the square brackets within single or double quotes. I am using localhost so my IP address is 127.0.0.1

,,,,settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@%46-rdvjoh2=)@10$yimeo10m(dn&w^*9n*rcv5fr)!1nbh5c'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Edit the line below with your server IP address
ALLOWED_HOSTS = ['your-server-ip']
#ALLOWED_HOSTS = ['127.0.0.1']


# Application definition

With this completed, be sure to navigate back to the directory where manage.py is located:

$ cd ~/django-apps/testsite/

Now, run the following command

$ python manage.py runserver

For first time it will ask you to migrate.

$ python manage.py migrate

Then run the server again with python manage.py runserver

Your app address will be (http://your-server-ip:8000/) . In my case its 127.0.0.1:8000

How to install Django on linux, Mac and Windows
Django Default Page

This is to confirm that Django is properly installed and our test project is working correctly. When you are done testing your app, you can press CTRL + C to stop the runserver command. This will return you to your programming environment.

When you are ready to leave your Python environment, you can run the deactivate command:

$ deactivate

Deactivating your programming environment will put you back to the terminal command prompt.

Conclusion :

In this tutorial you have successfully upgraded to the latest version of Python 3. You’ve also installed pip 3, virtualenv, and django. Now you have the tools needed to get started building Django web applications.

If you face any difficulties/errors during the process, feel free to comment we will try our best to solve your issue. Thank You 🙂.

5 Comments

  1. Pretty! This has been an extremely wonderful article. Thanks for supplying this info. Aeriel Alfred Sven

  2. You ought to take part in a contest for one of the most useful sites on the net. I most certainly will recommend this web site! Mallissa Alaric Hieronymus

  3. Learning curve hypotheses prototype early adopters focus channels direct mailing business-to-business vesting period. Equity seed round funding advisor partnership vesting period channels niche market social media business plan long tail. Startup deployment partner network holy grail pivot bootstrapping product management accelerator virality churn rate business-to-consumer network effects seed round. Influencer client startup. Jandy Sanson Browning

  4. I have a spin mop similar to this one and absolutely love it. I have never been fond of using my hands to ring out a mop. Kial Alisander Slavin

  5. Hey there. I discovered your site by way of Google at the same time as looking for a comparable topic, your web site came up. It appears to be good. I have bookmarked it in my google bookmarks to visit then. Lissie Adelbert Romelle

Write A Comment