[Django] A brief before starting a project

What is Django ? When do we need it

Django is a Python-based free and open-source web framework, which follows the model-template-view or model-controller-view architectural pattern.
To be honest, big Django projects can be very complex beasts. So when we should use it? There are some cases:

  1. Data guys who need a basic interface to interact with database, dataset
  2. Your company want to make a internal management tool to get rid of boring procedures
  3. The design teams needed to be able to manage the user experience with the tools they already had (HTML, CSS, JavaScript etc.)

Django’s architecture

  • Models are the tools we use to work with data and databases
  • Templates provide a designer friendly plain-text templating system
  • View

as I mentioned before Django is Model-View-Controller (MVC) framework and Model-Template-View (MTV) also. A common way of explaining Django’s architecture in terms of MVC is to describe it as a Model-Template-View (MTV).

  • Model(MVC) = Model(MTV)
  • View(MVC) = Template(MTV)
  • Controller(MVC) = View (MTV)

View

the view retrieves data from the database via the model, formats it, bundles it up in an HTTP response object and sends it to the client (browser). In other words, the view presents the model to the client as an HTTP response

Remember that you will mess up the whole project if you try to bend the definition of Django View for cutting edge by put everything in views module. Do not do it

Again, do not do it !!!

Let’ break a view module into 2 part : view logic and app logic.

If a function or class return a response –> it is a view –> put it in view logic (views.py)

If a function or class not return a response –> it is not a view –> it is app logic —> put it in another .py file

There is a middleware which sits between the view and the client-side. Django’s middleware performs critical security and authentication checks before the response is sent to the browser.

Django Project Skeleton

[projectname]/                  <- project root
├── [projectname]/              <- Django root
│   ├── __init__.py
│   ├── settings/
│   │   ├── common.py
│   │   ├── development.py
│   │   ├── i18n.py
│   │   ├── __init__.py
│   │   └── production.py
│   ├── urls.py
│   └── wsgi.py
├── apps/
│   └── __init__.py
├── configs/
│   ├── apache2_vhost.sample
│   └── README
├── doc/
│   ├── Makefile
│   └── source/
│       └── *snap*
├── manage.py
├── README.rst
├── run/
│   ├── media/
│   │   └── README
│   ├── README
│   └── static/
│       └── README
├── static/
│   └── README
└── templates/
    ├── base.html
    ├── core
    │   └── login.html
    └── README

apps/

[projectname]/                  <- project root
├── *snap*
├── apps/
│   └── __init__.py
└── *snap*

This directory is used for custom applications. You can safely remove this directory, if you do not plan to develop custom applications. Most of a Django project’s apps will be installed into the Python path and not be kept in your project root.

configs/

[projectname]/                  <- project root
├── *snap*
├── configs/
│   ├── apache2_vhost.sample
│   └── README
└── *snap*

doc/

[projectname]/                  <- project root
├── *snap*
├── doc/
│   ├── Makefile
│   └── source/
│       └── *snap*
└── *snap*

This directory contains the source files for this documentation. You can safely remove this directory, if you just want to use the skeleton for your own project.

run/

[projectname]/                  <- project root
├── *snap*
├── run/
│   ├── media/
│   │   └── README
│   ├── README
│   └── static/
│       └── README
└── *snap*

This directory contains necessary files for running Django.
This directory will contain the SQLite database file

static/ and templates/

[projectname]/                  <- project root
├── *snap*
├── static/
│   └── README
└── templates/
    ├── base.html
    ├── core
    │   └── login.html
    └── README

These directories are used for project wide files, meaning project wide static assets and templates.

static/

[projectname]/                  <- project root
├── *snap*
├── static/
│   └── README
└── templates/
    ├── base.html
    ├── core
    │   └── login.html
    └── README

his directory is used to provide our project wide static assets

templates/

This directory is used to provide our project wide templates.

A nature, universe, science, music, love lover

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami