Django is an open-source web application framework for Python that follows the Model-Template-View architecture, enabling the development of dynamic websites and applications. It provides a unified API for different databases and includes components like an object-relational mapper and a templating system. The document outlines Django's architecture, project structure, steps to create a new project, and the files generated during project initialization.
Django is an open-source web application framework for Python that follows the Model-Template-View architecture, enabling the development of dynamic websites and applications. It provides a unified API for different databases and includes components like an object-relational mapper and a templating system. The document outlines Django's architecture, project structure, steps to create a new project, and the files generated during project initialization.
Django is an open source web application framework for developing web applications in Python. A web application framework in general is a collection of solutions, packages and best practices that allows development of web applications and dynamic websites. Django is based on the Model-Template-View architecture and provides a separation of the data model from the business rules and the user interface. Django provides a unified API to a database backend. Thus web applications built with Django can work with different databases without requiring any code changes. With this flexibility in web application design combined with the powerful capabilities of the Python language and the Python ecosystem, Django is best suited for cloud applications. Django consists of an object-relational mapper, a web templating system and a regular-expression based URL dispatcher.
5.6.1 Django Architecture:
Django is Model-Template-View (MTV)framework. Model: The model acts as a definition of some stored data and handles the interactions with the database. In a web application, the data can be stored in a relational database, non- relational database, an XML file, etc. A Django model is a Python class that outlines the variables and methods for a particular type of data. Template: In a typical Django web application, the template is simply an HTML page with a few extra placeholders. Django‗s template language can be used to create various forms of text files (XML, email, CSS, Javascript, CSV, etc.) View: The view ties the model to the template. The view is where you write the code that actually generates the web pages. View determines what data is to be displayed, retrieves the data from the database and passes the data to the template.
Figure 5.12: DJANGO ARCHITECTURE
MRITS/II-II/CSE-IoT/Sensors and Devices 15
Python Web FrameWork UNIT-V
5.6.2 Project Structure:
A Django Project when initialized contains basic files by default such as manage.py, view.py, etc. A simple project structure is enough to create a single-page application. Here are the major files and their explanations. manage.py- This file is used to interact with your project via the command line(start the server, sync the database… etc). For getting the full list of commands that can be executed by manage.py type this code in the command window- $ python manage.py help
_init_.py – It is a python package. It is invoked when the package or a module in the
package is imported. We usually use this to execute package initialization code, for example for the initialization of package-level data. settings.py – As the name indicates it contains all the website settings. In this file, we register any applications we create, the location of our static files, database configuration details, etc. urls.py – In this file, we store all links of the project and functions to call. wsgi.py – This file is used in deploying the project in WSGI. It is used to help your Django application communicate with the webserver.
5.6.3 Steps to create New Project:
_ Create a project _ Start an application _ Create the database (MySQL, Postgresql, SQLite) _ Define DB Settings in Settings.py _ Define your models _ Add pluggable modules _ Write your templates _ Define your views _ Create URL mapping _ Test Application _ Deploy Application (Linux, Apache, mod_Python, DB)
MRITS/II-II/CSE-IoT/Sensors and Devices 16
Python Web FrameWork UNIT-V
5.6.4 What Django generates:
_ MySite/ _ __init__.py _ Manage.py // Script to interact with Django _ Settings.py // Config _ URLs.py$ // My Site URL mapping _ MyProject/ _ __init__.py _ URLs.py // Project specific URL mapping _ Models.py // Data Models _ Views.py // Contains the call back functions _ Admin.py _ Templates