DEV Community

Cover image for Let's Learn Django #1 Init
Rachit Khurana
Rachit Khurana

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Fish Shell (Linux)

source ./venv/bin/activate.fish
Enter fullscreen mode Exit fullscreen mode

CMD (Windows)

venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

Powershell (Windows)

venv\Scripts\Activate.ps1
Enter fullscreen mode Exit fullscreen mode

MacOS

source ./venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

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

pip install Django
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

For eg.

django-admin startproject triviaquiz
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

For eg.

django-admin startapp quiz
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Default Django home page

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

Top comments (0)