How to Set up Your First Django Project: A Beginner's Guide

donesrom

Donesrom

Posted on January 8, 2024

How to Set up Your First Django Project: A Beginner's Guide

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. 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

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

command prompt

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


Enter fullscreen mode Exit fullscreen mode

VirtualEnv is a tool that allows you to create isolated Python environments.

2.Create a folder/directory
Type,



mkdir dir_name


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Replace source with the name of your virtual environment:

virtual environment

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


Enter fullscreen mode Exit fullscreen mode

2.Create a virtual environment

Run the following command in a terminal window:



virtualenv my_project


Enter fullscreen mode Exit fullscreen mode

3.Activate your Virtual Environment

Run the following command in your terminal window:



source my_project/bin/activate


Enter fullscreen mode Exit fullscreen mode

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.

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


Enter fullscreen mode Exit fullscreen mode

Linux

Type the following command on your terminal window



pip3 install django


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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>


Enter fullscreen mode Exit fullscreen mode

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>


Enter fullscreen mode Exit fullscreen mode

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>


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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

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.

💖 💪 🙅 🚩
donesrom
Donesrom

Posted on January 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related