Django is one of most widely used Python web development framework and powers several popular applications. In this article, we have explored the structure of a Django web application. It follows Model, Template and View (MTV).
What is Django
Django is free and open source web application framework, written in Python. The official project site describes Django as "a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Ok cool !,but What is Web Framework
Web Framework is set of components which provide you tools and libraries to simplify the website development.So for every web application you need some sort of same actions like authetication ,admin panel for maintaining the whole website.
So the Web Framework is used to make your development process a lot easier.
Some of the Features of Django
1.Rapid Development : Django was designed to help developers take applications from concept to completion as quickly as possible
2.Secure : Django provide secure way to store the user details .Used cryptographic hash function for encrypting the password.
3.Scalable :It can be scaled for more traffic using more hardware components.
4.Maintainable: Django is designed such that it follows the DRY(Don't repeat yourself )means removing redundant code.Maintaining the reusable code.
5.Versatile : Django can be used to build any type of website.From ecommerce site to online coding site ,social site etc.
7.Open Source : This is open source project.
8.Vast and Supported Community
Question how django tackles the request from the user
Django's architecture consistes of three major parts:
We say that MTV( Model,Template and View )
Django Models : is logical tools which we use to work with data and databases
Django Templates : what we see in the browser ,user friendly templating system.
Django Views : handles the communication between user and the database.
Request comes to the web server .It takes the url patterns and try to resolve.It takes a list of patterns and tries to match the URL. Django checks patterns from top to bottom and if something is matched, then Django passes the request to the associated function (which is called view).
View retrieves the data from the database via model and then perform the action ,like user want to add his/her information in database the view interact with model and model interact with the database.
View will return the HTTP response object and serve to client side browser.
Client side browser ,the response returns from the views ,get renders into the HTML templates.
This is how all this thing happens:
Other things that django can do:-
User Authentication and Permissions
Forms : HTML Forms are used to collect user data for processing on the server. Django.Django simplifies the form creation .
Admin site : It makes it easy to provide an admin page for site administrators to create, edit, and view any data models in your site.
Caching: Creating the content dynamically is intensive than static content.Django solves this problem using caching.
Serialising data: Django makes it easy to serialise and serve your data as XML or JSON.
How the Django Project Structure Look Like?
Basic unit of a Django web application is a Django Project. A Django Project is made up one or more Django apps.
- App 1
- App 2
- App 3
- ....and more if made
Django app contains all the package .If the developer is creating a product site which includes blogs also so the blog is contained in the app which includes all the functionalities like models,views and
So the Project Structure of Django : =
\venv \project_folder_name // Here is django \project_folder_name // Django app db.sqlite3 // Project Database manage.py // Command utility
venv is the python virtual environment. A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.
Manage.py command-line utility for exceuting django command from within your project
db.sqlite3 The database ,this is created when we run migrate command within the project
(inner)project_folder_name is the django web application.
In the inner project folder
\project_folder_name __init__.py settings.py urls.py wsgi.py
init.py this tells that the folder is a python package.
settings.py contains the settings for your django project.Every django project must have a settings file.
urls.py contains project-level URL.Like Url pattern for admin.
wsgi.py compatible with web servers to serve your project.ALthough this file is not being used during development.
Creating Django apps
python manage.py startapp app_name
This will create the app inside the root project_folder which includes : -
\app_name __init__.py admin.py apps.py models.py tests.py views.py
init.py tells python that your app is package.
admin.py here you register your app's models .
apps.py is a configuration file.
models.py all the models for the app.
tests.py contains test procedures that will be run when testing your app
views.py where you define the logical method for interacting with models.
so now the whole Django Project look like : -
\venv \project_folder_name // Here is django \project_folder_name // Django app __init__.py settings.py urls.py wsgi.py \app_name //the project app (ex- blog ,or ecommerce etc .) __init__.py admin.py apps.py models.py tests.py views.py db.sqlite3 // Project Database manage.py // Command utility
All the other things like static files CSS and Js .stored in the static folder.
The configuration for static files updated in the django settings.py.
For installing the django in your local desktop ,you need to use pip.
Companies that use Django include: Disqus,Udemy,Leetcode, Mozilla, Knight Foundation, MacArthur Foundation,National Geographic, Open Knowledge Foundation, Pinterest, and Open Stack
This is all about the django application structure.You can find many tutorials onine for learning django.Time to grab some cofee,Build your first django web app.
For more documentation of Django :- You can find it on the official site
With this article at OpenGenus, you must have the complete idea of the Application structure of Django. Enjoy.