Let's Learn Django #1 Init

Let's Learn Django #1 Init

This is a blog series about Django from the very basics

Installing & initialising a project

Django is a really powerful Python-based web framework with the philosophy batteries included. It means it gives you everything you need to build a full-stack web application. Django's Tag line is The web framework for perfectionists with deadlines. It's because it allows you to create professional-grade web apps very quickly.

However, learning it may seem a bit complex, but believe me, it is not. And to help with that, I'm writing this blog series.

So let's start from the very basics, installation & initialising a project

Installation

Installing Django is pretty straightforward. You just need to use pip to install Django. However, it's always recommended to use a virtual environment for a project. So we will start by creating a virtual environment.

python -m venv venv

This will create a virtual environment in the venv folder. To use this environment, we need to activate it. Activating it is different in Windows, Linux & MacOS.

Bash Shell (Linux)

source ./venv/bin/activate

Fish Shell (Linux)

source ./venv/bin/activate.fish

CMD (Windows)

venv\Scripts\activate.bat

Powershell (Windows)

venv\Scripts\Activate.ps1

MacOS

source ./venv/bin/activate

After activating the virtual environment, we can install Django using pip.

pip install Django

Of this, we have successfully installed Django on our systems. So what are we waiting for? Let's start a project

Initialising a project

To start a project, we will use the django-admin command. It will help us generate the boilerplate code.

django-admin startproject <projectname>

For eg.

django-admin startproject triviaquiz

This will generate a boilerplate code for you. It will include a lot of files like manage.py, settings.py, models.py, urls.py etc.

Now to organise our code, we divide our Django web app into several smaller sub-apps. To create a sub-app, we will again use the django-admin command.

django-admin startapp <app_name>

For eg.

django-admin startapp quiz

This will create a new folder with several other files like models.py, views.py, admin.py etc.
You can try exploring all these files yourself as Django provides comments in the boilerplate code for easy understanding.
However, I will also explain each of them in detail in my next blog.

Running the project

To run the project, we will use the following command:

python manage.py runserver

This will start the server with your project at http://127.0.0.1:8000.
And by going to this url, you will a default Django page like this:

If you are seeing this, then congrats, you have successfully set up & started your project.

In the next blog, I will explain all the different Django files in detail. Stay Tuned

Follow my journey on Twitter: @notnotrachit

Did you find this article valuable?

Support Rachit Khurana by becoming a sponsor. Any amount is appreciated!