Django Frameworks Python

How To Create A New Django Project In Windows Using Powershell

Django an open-source python based web framework.

It follows the model-view-template design pattern as opposed to the model-view-controller pattern that PHP web frameworks like laravel uses.

Django is developed and maintained by the Django Software Foundation which is based in the United States.

According to the Django official website, Django is a high-level Python programming web framework that encourages rapid development and its used mainly for pragmatic designs.

In this tutorial, I am going to show you how to set up your Django project in windows operating system using Windows Powershell

Windows Powershell is the Linux command prompt equivalent for Windows. it means that you can write almost the same commands you normally write on Linux now on windows.

Instead of using the normal Windows command prompt to create our Django project, we shall be using the Windows Powershell.

Powershell can be found in every windows machine starting with Windows 7 SP1.

So to follow along in this tutorial, you need to be sure that your windows machine has windows 7 and higher running in it.

I will be using Windows 8 to demonstrate how you can go about it and the process is almost the same in all the various versions of the Windows operating system.

Step 1:

move your mouse down the bottom left of your screen. The start menu will pop up. Right-click on the start menu and select search.

Once the search opens, then type Powershell. In other versions of windows just enter Powershell in the search button and it will come up.

Open the Powershell and you will see and it should look like the image below.

 

Step 2:

Now that we have opened Windows Powershell, we are going to head over to the official Python website and download python. Python installation is required for us to be able to install Django.

Python has version 2.x.x and version 3.x.x

Python version 2.x.x is no longer been supported by the Python Software Foundation. Python Software Foundation ended support for Python 2.x.x on January 1st, 2020. Python 3.x.x is what is currently been used and all projects executed in Python 2.x.x are currently been migrated to Python 3.x.x

Django works with Python 3.x.x and that is what we are going to install.

 

Once you have opened it, click downloads and download Python for Windows. Open it and follow the installation process.

During the installation, tick the options that come up. most importantly the ADD TO PATH option. This will add Python to the Windows PATH. It is a very important process. Wait until the installation is complete. Once it is complete, you can close it.

You are most likely going to see these options that you must tick depending on your Windows version. There is no particular order to it but once you see it then tick them.

Tick/Select Add Python 3.6 to PATH
Select Customize Installation (this is important)
Tick/Select pip (others, leave as default)
Hit next
Tick/Select:
Install for all users
Add Python to environment variables
Create shortcuts for installed applications
`Precomplie standard libary
Customize Install Location and use: `C:\Python36
Hit Install

Confirm that the installation was successful by heading to Windows Powershell and type

> python –version

You will see the version of the Python you installed. In my case, I installed Python version 3.8.3

 

type also > python -v

This is to verify that Python is working. Typing python -v will start the Python compiler and you can then write Python codes on Windows Powershell.

If the Python compiler does not start, then you have to uninstall the Python and re-install it again or try restarting your computer.

If in your case it is working. then try and carry our some mathematical operations like

1+1, 3-4 etc. It should give you some results.

To close the compiler, type

>>> quit()

and the compiler will stop.

Another thing we are going to verify is pip

pip is a standard package management system used to install and manage packages or modules written in Python.

pip comes python default with Python version 3.4.x and above.

we are going to use it in install Django and its packages and modules it

To confirm that pip was installed correctly, type in Windows Powershell

> pip freeze

If you see The term ‘pip’ is not recognized as the name… then pip was not correctly installed and the problem will mostly be coming from not ticking pip during the Python installation. Other than that, pip was correctly installed.

Step 3:

This next setting you will have to do it just one time. Close your Windows Powershell if it is still open and then search for it again.

Once the Powershell comes up, right-click on it and then select “Run as administrator’. Confirm any security pop up if need be. This will open Windows Powershell but this time with administrator privileges. Then in Windows Powershell type

> Set-ExecutionPolicy Unrestricted

and then hit enter. Once you are done, you can close Windows Powershell.

Step 4:

Search and Open Windows Powershell again. This type not as Administrator as we did in the previous step.

Now that we have Python installed, it time for us to install Django on Windows using Powershell.

We are going to move to the directory where we want to install Django. For me, I want to install Django in my downloads directory. You can install Django in any directory of your choice. It is a good practice to always have a particular directory where you can install all your Django projects.

Let me teach some Windows Powershell commands you will find very helpful at this point.

a. ls = it means list.

> ls

once you type ls in Windows Powershell, it will list all the files and folders you have in that directory.

b. cls = it means clear

> cls

c. mkdir = it means ‘make a directory’

> mkdir

d. cd.. = this is to move back to the previous folder

> cd..

So I will type in Windows Powershell

> cd downloads

or I could first of all type

> ls

So I can see all my directory before typing cd <folder name>. Remember that <folder name> is the folder of your choice where you want to install Django.

I could as well make a new directory where I could be installing all my Django projects by typing in Windows Powershell

> mkdir <folder name>

 

Step 5:

Now that we are in our desired folder we are going to create a Python virtual environment.

What is a Python virtual environment?

A python virtual environment is an isolated environment for a Python project. we can create python projects that are independent of other python projects and they have their own dependencies regardless of whatever dependency other python projects are using.

A Python virtual environment is necessary because, with it, we can create many Django websites that are independent of the other.

Head over to Windows Powershell and type

> mkdir djangotutorial

Remember you can choice any folder name of your choice > mkdir <folder name>

after that type

> cd djangotutorial

To create a virtual environment in our djangotutorial folder type

> virtualenv .

take a very important note of the dot after virtualenv, this command should create your Python virtual environment.

If you have Python 2 and Python 3 installed in your computer, you may have to type this

> virtualenv -p python3 .

In other to use the Python Virtual Environment, we are going to activate it. Type

> Scripts\activate

You can also type this

> .\Scripts\activate

Just use whichever one is very convenient for you. I normally use the first one.

You should see (djangotutorial) at the beginning of the file path.

I had to type

>ls

before activating the Python virtual environment so I can see the files that were generated when I created the Python virtual environment.

at this stage type

> pip freeze

pip freeze should return nothing at this point. This is to show that it is an isolated python environment.

To deactivate the Python virtual environment, type this

> deactivate

This command will deactivate the Python virtual environment. If you want to activate it again, then follow the steps mentioned earlier.

Step 6:

Now that we have installed Python 3, created and activate our Python virtual environment, it is time for us to install Django.

Head over to Windows Powershell and move to the djangotutorial folder we created (or the folder you created). activate the Python virtual environment and then type

> pip install django

This command should install the latest version of Django. However, if you want to install a specific version of Django, use the command below.

> pip install django==3.0.7

==3.0.7 is the version of Django you want to install. There are some cases that you would have to install a specific version of Django for a specific project. So you can replace the numbers accordingly based on the Django version you want to install.

 

Step 7:

Now that we are done with installing Django, we are going to create our first Django project.

The name of our Django project will be ‘blog’. So keep your virtual environment activated. Then type

> django-admin startproject blog

 

you should notice that a folder called ‘blog’ was created when we ran that command.

now move into the blog folder by typing

> cd blog

Then so we can run our Django project on our web browser, type

> python manage.py runserver

This command will start the server at localhost:8000 or http://127.0.0.1:8000

 

finally, head over to your browser and type localhost:8000

your Django website should be running at this point

 

if you want to stop the Django server from running, type Ctrl c, and the server will automatically stop running.

Below is a summary of all the Windows Powershell command you would have to write.

> cd download

> mkdir djangotutorial

> cd djangotutorial

> virtualenv .

> Scripts\activate

>django-admin startproject blog

>cd blog

> python manage.py runserver

If you encounter any challenge during your installation, then leave a comment below and I will gladly help you out.

Share this blog post

thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *