Hello Devs, Welcome to the ocean of Web Development with Django. In this blog, we have discussed all the information that you need to know before you start surfing on this ocean. Mainly, 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 by developers with a view to 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 was one of the best guitarists of all time.

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. It creates table in our database, and the fields of the class creates the attributes of the table. Models are inside of an app’s ‘models.py’ file. 

View: View is the bridge between a model and a template. We write all the logic to process data of a model and the data to be sent to a template in views. We use python function or python 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. Basically, the template layer is the presentation layer.

Django MVT Model
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 do not. So, you do not 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‘. We can only use ‘venv’ 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

And iIf you use ‘venv‘ then it be…

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 are same in both cases. ‘virtualenv’ or ‘venv’ :

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 

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

Besides, there is another structure of 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.You can create an independent module for each functionality. 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>

And this command 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', 

     #my_apps
    'app_name',
] 

Making Migrations

To convert the Django Models into Database Table Django uses an Object-Relational-Mapper (ORM). When we 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 having  all the permissions and full control over full admin panel of a website. If you log 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

Lastly and most importantly, run your  django project in your local server ( localhost:8000 or http://127.0.0.1:8000/). Execute the following command. Again remember that you need to be in the directory where manage.py is present.

python manage.py runserver

Now you willl 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.

Finally, you have all set to start your project. Go to next article for your next move.

+ posts

Author | Python-Django Developer

+ posts

Full-stack Developer (Python | Django | React | React-Native | Angular | Vue)