The Django Sea Tutorial: Setting up the environment for Django project

Akanksha Verma
The Techie Trio
Published in
7 min readJun 10, 2020

This Article is contributed and reviewed by Prakhar gupta, Ishan Rawat, and Akanksha Verma.

To whom it may concern(YOU, obviously!),

It’s been long since we’ve not written anything after our debut on medium.

The reason is: WE ARE LAZY.

Too lazy to move our fingers around our keyboards, too lazy to build up any content, too lazy to move our fat tummies to sit and work.

We believe everybody loves being lazy.

So, talking further on this, our lazy attitude continues to be exhibited in our coding patterns. We dream to have an insane output by writing just 100 lines instead of the usual 10,000. (Though that’s not possible :P)

What an irony!

But, shortening the amount of code to write is indeed an excellent optimization and is possible to some extent(Now, we make some sense!).

This is what frameworks are for! They have the powers of doing this along with some other excellent features.
For a better definition of a framework, it is an application that provides various features to create other custom applications.

By this lazy discussion, we come to our topic: Django(nice approach, huh?).

An awesome backend framework in python which is ridiculously fast, highly secure, and has appreciable features.

What we are gonna do is(hopefully)- help you start with your first Django Project and make a basic website with this framework.

With that in mind, it’s time to dive into Django, but thinking of diving without a swimsuit gives us the creeps!

Well, what we mean is that the prerequisite (metaphorically called the swimsuit) for starting Django development is learning one of the most exciting languages-Python.

Make sure you already have some basic knowledge of python.

And after that, you would have completed the training session for diving. Now, let’s splash some water in the real sea!

What is Django?

Django is a backend framework, written in Python. It encourages rapid development and was framed to help developers code readily with ease as it provides inbuilt features like awesome templates, additional security, and a very lightweight server for testing purposes.

The official documentation of Django is available here.

Getting Started

Now, hopefully, we take some speed😅

Python Virtual Environment

Our Django project has some dependencies which are needed for the proper functioning of the project. No, no we won’t be talking of the dependencies but about a way of their maintenance. We maintain something called a virtual environment for separating dependencies between projects.

But why so?

Different versions of Django may require different versions of the same dependencies.

Hence, if you build two web apps with different versions of Django, conflict will occur in running the apps since python can keep only a single version of any dependency in the system.

Virtual environments solve this problem by creating isolated environments for each project where the dependencies of one project remain isolated from the other.

How are we going to do this?

We use a module named virtualenv which is a tool to create isolated Python environments. It creates a folder containing all the necessary executables to use the packages that are required by the Python project.

Installation Process

We would also require python 3.6 for Django as well as the virtual environment.

To check the version of Python 3 in your laptop enter this command:

python3 --version

If the version 3.6 is not present, paste the below command in the Linux terminal:

sudo apt install python3.6

Now if you check the version, python 3.6 would be present on your system.

Now comes the time for creating a folder for our Django project!! (Yippee!!!)

mkdir foldercd folder

To install virtualenv we will use pip3 (pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes), To install pip3:

sudo apt-get install python3-pip

Now install virtualenv using pip3 (Huhh, finally!!)

sudo pip3 install virtualenv

After all these efforts, we create a virtual environment using this simple command:

virtualenv venv

To activate the virtual environment for further working:

source venv/bin/activate

You would notice (venv) written before your username in the terminal. Next time onwards, all you need is to run this command in order to activate the existing virtual environment.

Installing the Django development version

Check out Django’s main development branch:

git clone https://github.com/django/django.git

Install Django:

$ python -m pip install -e django/

Verifying the installation

Open shell and type

python3>>> import django
>>> print(django.get_version())
3.0

So, we downloaded the version 3.0 of Django!

Saving Dependencies

We save all our dependencies in a file called as requirements.txt. No explanation needed, we guess: the name of every dependency stored in a single file.

pip3 freeze > requirements.txt

Coming to the use of this file, if someday, somebody clones your project (let’s say you put in on Github), all he needs to do to install the dependencies is run this:

pip3 install -r requirements.txt

This would install all the dependencies listed in the file.

Hey! Setup done within two minutes! ( Maggi!!).

Getting away from food is tough! But anyways, coming back, whenever you want to exit out of the virtual environment, enter the following command:

deactivate

And so with such a huge array of commands, we (to summarize not to conclude!) installed python 3.6, made a virtual environment, and then installed the deadly demon Django.

Also, if you have come till here we must stop here and clap for you(for there are only a few with the almighty power to bear us :P)

Okay! Back to work.

Now, comes the time to start the project! (OH, yeah!)

Setting up the basis

We start with writing this little mighty command:

django-admin

After hitting enter, you would see a set of sub-commands available. Each has their own use, some of which you will learn in this blog.

Somewhere in the list, you’ll see the startproject sub-command.

As implied by its name, we use it to create a new Django project-

Type :

django-admin startproject SocialNetwork

Was something done? Yes, here’s what happened:

  • A project named SocialNetwork is created.
  • Do cd SocialNetwork and see the project structure that Django has created for us.

The structure seems like this -

  • The empty __init__.py tells that it is a python package.
  • As the name suggests settings.py has relations to settings of your Django project.
  • django-admin is Django’s command-line utility for administrative tasks. manage.py is the same but in addition, also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file. What this additional step does is it specifies what the “settings” of your project are.
  • urls.py is where we map URLs to different locations in our app. It would be looking somewhat like-
urlpatterns = [
path(‘admin/’, admin.site.urls)
]

If you try to interpret this a bit you would see that Django provides us with a route to admin page(where admin/ is mapped up with admin.site.urls i.e. an inbuilt file) which can be accessed by localhost:8000/admin (but for that we need to start Django's built-in server which runs on port 8000 by default, the instructions on how to start that will be explained super soon!).

After a few paragraphs, we would ourselves create some routes in a similar manner!

Starting the server

The super soon part came! Aren’t we very fast at completing our promises?

So, to start the server type these two commands first(will be explained later):

python manage.py makemigrations
python manage.py migrate

Now start Django’s inbuilt server, with the command(make sure you are the same directory as manage.py):

python manage.py runserver

The server would have started(hopefully!)

Now, open your browser and type:

localhost:8000

You would see a rocket there. Yayyyyy!!!

And in another tab, type:

localhost:8000/admin

The admin page is one of the pages that Django automatically creates for us. The logic for this page is predefined.

Before we end the blog, a note(the end came very fast!):

In Django, we generally make a Project(with startproject sub-command) which further contains multiple apps(made with startapp sub-command which is yet to be shown). Different apps are made to separate logics.

Also, a Django app is a group of related functionality used to complete or maintain one aspect of a site.

In this series, we will make 2 apps inside the main SocialNetwork project named “blog” and “user”.

  • Blog app contains the logic for the blog website.
  • User app contains the logic for user login and logout.
  • They both are apps inside the SocialNetwork Project.

Final project structure would be like(note that db.sqlite3 would be automatically created when you run the project)-

P.S. The next blog of this series where we continue our Django project is here.

This was just self-promotion😛.

--

--