How to Set up Your First Django Project: A Beginner's Guide
Tags: webdev, python, django, tutorial
Published: 2024-01-08 15:14:09

In your journey to learn Python and web development, you might have come across Django as a preferred tool by many experienced developers for building websites. You might also have learned that some of the biggest companies in the world, such as Netflix, run on Django. 

Python learners who wish to venture into web development rely on frameworks such as Django and Flask to build web applications.

Django is a high-level Python framework designed to help you create scalable, neat, and maintainable websites. It is a free and open-source technology that will help you avoid all the hassle of building useful websites. 

This guide is designed to help Windows and Linux users set up their first Django projects. 
## Quick Steps
If you are coming back for reference:

Step 1: Create a Virtual Environment
Step 2: Install Django
Step 3: Create a Folder
Step 4: Create a Project
Step 5: Set up Your Server
## Prerequisites 
You need to have the following for this project to work. 

1. Python. It helps if you have the latest version of Python. 
2. Pip, a package manager for Python.
3. A virtual environment. 

Read this article to learn [how to create a virtual environment for both Windows and Linux users](https://dev.to/donesrom/how-to-create-a-python-virtual-environment-in-windows-and-linux-92l). You will also learn how to install and use pip. 

## Getting Started
It is important to note that you must do most things, including creating actions and commands through code. Most of your code will go to your preferred code editor.


![preferred code editors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ce1tn0vnnz624h2kyuds.png)

The other code will come in simple instructions on your command prompt (CMD) or terminal.

![command prompt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhkvjvdiq8x0b1g063z9.png)

The CMD/terminal is where you execute most of your commands and installs for the project to run.

### To access CMD for windows
Type cmd on the search box and the bottom left side of your screen. Pick the command prompt App option.
### To access your Terminal on Linux
Click on the Launchpad icon on the dock, search for Terminal on the search box, and then click on the Terminal option.

> Note: It is possible to run your commands through your code editor. Some code editors also provide you with a functional terminal where you can run your installs and commands.

**However, learning how to run your Django project in CMD is also important.**
## Step 1: Create a Virtual Environment
A virtual environment is a container that holds your project and prevents it from mixing up with other projects on your computer. Think of it like a shelf with many levels. Your computer is the whole shelf, while a virtual environment is an individual shelf. Here, you can add books from particular authors in a way that makes the most sense to you.

Learning how to create a virtual env will make it easier for you to create independent projects. 
### To create a virtual environment on Windows:

1.Run the following command on your terminal window:
```
python -m pip install --user virtualenv
```
VirtualEnv is a tool that allows you to create isolated Python environments.

2.Create a folder/directory
Type,
```
mkdir dir_name
```
_Replace dir_name with the name of your project’s directory._

3.Create your virtual environment
Run the following command in a terminal window:
```
mkvirtualenv source
```

_Replace source with the name of your virtual environment:_


![virtual environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a10wtxz5915ih511prs.png)

> Note: Windows will create and activate your virtual environment at the same time. However, you must activate your virtual environment once you exit the terminal.

### To create a virtual environment on Linux:

1.Install VirtualEnv

VirtualEnv is a tool that allows you to create isolated Python environments. You can install it by running the following command in a terminal window:

```
sudo pip3 install virtualenv
```

2.Create a virtual environment

Run the following command in a terminal window:

```
virtualenv my_project
```

3.Activate your Virtual Environment 

Run the following command in your terminal window: 

```
source my_project/bin/activate
```

_Replace my_project with the name of your virtual environment._

This will create a new directory called “my_project” in your current working directory. This directory will contain all the files and packages needed for your virtual environment.

Remember, I have written a more detailed guide on creating a virtual environment in Windows and Linux [here](https://dev.to/donesrom/how-to-create-a-python-virtual-environment-in-windows-and-linux-92l). 
## Step 2: Install Django
This installation allows you to work with the latest version of Django.
### Windows
On your CMD/terminal, type:

```
pip install django
```

### Linux
Type the following command on your terminal window

```
pip3 install django
```

> Note: It is possible to work with other versions of Django if you like. Do this by adding your preferred version at the end of the pip command. For example,

```
Pip install django==3.7
```
## Step 3: Create a Folder
You will need to create a folder that houses the entire project. 
On your CMD/terminal, type:

```
mkdir <project name>
```
_Remember to replace with the preferred name of your project._

mkdir means Make Directory. It is a command used to create the parent folder for your project for both Linux and Windows users.

Type cd into your cmd/terminal to enter the directory you just created. (cd means change directory).
### Create a Project
Now that you are inside your folder, it is time to work with Django to create a project. On your cmd/terminal, type;

```
django-admin startproject <projectname>
```
This will create another folder/directory with your project. 

_Remember to replace <projectname with the name of your project._ 

To access the project, type:

```
cd <projectname>
```
## Step 5: Set up Your Server
Once you have your project in order, it is time to set up your server. The server translates what you do at the backend into something you can see on your browser.

On your cmd/terminal:

```
python manage.py runserver
```
This will give you the server address: http://127.0.0.1:8000/. This free production server will allow you to see what is happening as you build your code.

Copy the server address and paste it into your browser. You should see something like this.


![Set up Your Server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n54eltlmte7vr2ejr3g.png)

## Frequently Asked Questions (FAQs)
### 1. Why is Django so popular?

Some of the reasons why Djangois is so popular include:

- It is secure - Django provides security through different functionalities such as user account and password protection.
- It is Versatile - You can use Django to build ANY type of website and pair it to any other client-side framework, and it will work just fine.
- It is Maintainable - Django uses a Don’t Repeat Yourself (DRY) principle. Developers use this principle to eliminate duplication in writing code by grouping related functionality into applications that can be reused repeatedly.
- It has excellent Documentation - Django has excellent documentation, known as Django Documentation, that will provide all the answers effectively using this great framework.

### 2. Do I have to Install Django for Every Project
Preferably, Yes.

First, remember that we are creating a virtual environment and installing Django. This virtual env is only accessible for the particular project we are working on.

Therefore, if you were to start another project using a different virtual env, you would have to download Django again.

### 3. Why not download Django to our computers once and for all?
Awesome developers are forever improving Django, so there are always new versions. Django documentation will always let you know what version is the latest.

There also comes a time when you want to use a particular version depending on the project you are working on.

This is why we install Django in the virtual environment, not on our computers. This way, each project has its version of Django.
## Final Thoughts 
Setting up our first Django project can be a scary and exciting experience. It is even scarier when you start seeing errors on the terminal because you missed a step.

However, this should not stop you from pursuing your dream. Soon, you can create a project, build your first app, and even work on other Django projects with source code without returning here. Until then, happy coding.