Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

All You Need To Know: Before You Start Django Development

Hello Devs, Welcome to the ocean of Web Development with Django. This blog is packed with
all the information you need to know before you start surfing on this ocean. We will cover the
following topics:

 What Is Django?
 Working Architecture of Django.
 What is PIP?
 What is Virtual Environment
 Creating, Activating & Deactivating Virtual Environment
 Starting Django project
 Creating an App
 Making Migrations
 Creating a SuperUser
 The Development Server

What is Django?
Django is an open-source and high-level web application framework written in Python. Django is
specially designed for rapid and clean development. A web framework is a package of
components that helps developers to develop websites faster and easier without handling low-
level details. The framework is named after Django Reinhard, a gypsy jazz guitarist. He has been
called one of the best guitarists of all time. Django follows the “Model View Template” (MVT)
architecture which is slightly different from the (MVC) “Model Controller”.

Working Architecture of Django.


Django follows the Model-View-Template (MVT) architecture which is slightly different from
Model-View-Controller (MVC).

Model: is a structure for storing data. The model handles everything related to data. Such as:
data access, data validation, dealing with data of other models, etc. It is implemented as a python
class that creates table in our database, and the attributes of the table are created by the fields of
the class. Models are defined inside an app’s ‘models.py’ file. 

View: View is the bridge between a model and a template. All the logic to process data of a
model and the data to be sent to a template are written in views. It can be implemented using
python function or Class which receives web request and returns web response. Operations like
Create, Retrieve, Update, Delete are handled in view.
Template: Templates are HTML pages with some special syntax describing how dynamic
content will be inserted. How the data will be presented on a web page is defined in the template
layer. The template layer is basically the presentation layer.

Source: http://www.syntaxbook.com/cover/210-django-mvt

What is PIP?
'pip' is a tool, actually a standard package manager for python used for installing,
uninstalling and managing python packages. 

Command to install packages


pip install <package-name>

Command to uninstall packages


pip uninstall <package-name>

Virtual Environment
A virtual environment is an isolated place where all the necessary packages are
installed for an individual project. Assume that you have multiple projects in your
PC. One of them needs a package named “Pillow” but others don’t. So, you don’t
need to install it globally. Here, you need a virtual environment for each of your
projects where the packages needed only for that specific project will be installed.
Creating Virtual Environment
There are one package and one library to create virtual environment. One is
'virtualenv' & other is 'venv'. 'venv' is a package that can be used only with python-
3. 'virtualenv' is a library which is easier to use and has more functionality.

If you use 'virtualenv' library then the command to create a virtual environment
will be:
virtualenv name-env

If you use 'venv' then it will look like:


On Unix or Mac OS
python3 -m venv name-env

On Windows
python -m venv name-env

Activating Virtual Environment


The command for activating virtual environment created by 'virtualenv' or 'venv' is same:

On Windows
name-env/Scripts/activate

On Unix or Mac OS
source name-env/bin/activate

Deactivating Virtual Environment


On Windows or Unix or Mac OS

deactivate

Starting Django Project


After creating a virtualenv the next step is to create a django project. The command to create a
project is as follows:

django-admin startproject <project_name>


After the command is executed you will see this directory tree in your working directory...
[projectname]/
├── [projectname]/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

There is another way of structuring your django project.

django-admin startproject <project_name> .

Make sure you give a dot (.) after your project name. It will avoid creating a nested folder with
the same name of your project which can be a thing of confusion and irritation in working time.

After the command is executed you will see this directory tree in your working directory...
[projectname]/
├─├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
Creating an App
We use different functionality in a website. For each functionality, an app can be created as a
fully independent module. The benefits of using apps for different functionality is they are
reusable and loosely-coupled. To create an app in your project you need to go to directory
containing manage.py and from there enter the command :

python manage.py startapp <app_name>

This will create an App with the following files:

app_name/
├─migrations/
├───├── __init__.py
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py

After creating an app you will have to register it in the list named "INSTALLED_APPS" lies inside
project/settings.py file.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'app_name',
]

Making Migrations
To convert the Django Models into Database Table Django uses an Object-
Relational-Mapper (ORM). When we do any change in our model, Django tracks
the changes and creates database migration scripts (inside  /app /migrations/) to
migrate the changes in the database to match the model.
We run the following commands to define tables for those models in the database.
python manage.py makemigrations
python manage.py migrate

Note: Make sure you are in the directory that contains manage.py

Creating a SuperUser
Every website has one or more superuser/s. SuperUser is just an object of 'User'
model with all the permissions and full control over full admin panel of a website.
If you're logged in as a superuser, you will have access to create, edit, and delete
any object of any model. In django we use the following command to create a
super user.

python manage.py createsuperuser

The development server


To run your Django project in your local server( localhost:8000 or
http://127.0.0.1:8000/) run the following command. Again remember that you need
to be in the directory where manage.py is present.
python manage.py runserver

You’ll see the following output on the command line:


Performing system checks...
System check identified no issues (0 silenced).
Run 'python manage.py migrate' to apply them.
February 19, 2021 - 15:50:53
Django version 3.1, using settings project_name.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You might also like