Django - Overview PDF

Summary

This presentation provides an overview of the Django web framework. It covers topics such as installation, features, and applications. The document details the use of Django for efficient web development.

Full Transcript

1 Introduction Django is a web application framework written in Python programming language. It is based on MVT (Model View Template) design pattern. The Django is very demanding due to its rapid development feature. It takes less tim...

1 Introduction Django is a web application framework written in Python programming language. It is based on MVT (Model View Template) design pattern. The Django is very demanding due to its rapid development feature. It takes less time to build application after collecting client requirement. 2 Introduction By using Django, we can build web applications in very less time. Django is designed in such a manner that it handles much of configure things automatically, so we can focus on application development only. 3 History Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle. Django was released on 21, July 2005. Its current stable version is 3.0 4 Popularity Instagram Mozilla Disqus Pinterest Bitbucket The Washington Times 5 Features of Django Rapid Development Secure Scalable Fully loaded Versatile Open Source Vast and Supported Community 6 Rapid Development Django was designed with the intention to make a framework which takes less time to build web application. The project implementation phase is a very time taken but Django creates it rapidly. 7 Secure Django takes security seriously and helps developers to avoid many common security mistakes, such as SQL injection, cross-site scripting, cross-site request forgery etc. Its user authentication system provides a secure way to manage user accounts and passwords. 8 Scalable Django is scalable in nature and has ability to quickly and flexibly switch from small to large scale application project. 9 Fully loaded Django includes various helping task modules and libraries which can be used to handle common Web development tasks. Django takes care of user authentication, content administration, site maps, RSS feeds etc. 10 Versatile Django is versatile in nature which allows it to build applications for different-different domains. Now a days, Companies are using Django to build various types of applications like: content management systems, social networks sites or scientific computing platforms etc. 11 Open Source Django is an open source web application framework. It is publicly available without cost. It can be downloaded with source code from the public repository. Open source reduces the total cost of the application development. 12 Vast and Supported Community Django is an one of the most popular web framework. It has widely supportive community and channels to share and connect. 13 Django Virtual Environment Setup The virtual environment is an environment which is used by Django to execute an application. It is recommended to create and execute a Django application in a separate environment. Python provides a tool virtualenv to create an isolated Python environment. We will use this tool to create a virtual environment for our Django application. 14 Django Virtual Environment Setup Installing Package pip install virtualenvwrapper-win Making Virtual Environment mkvirtualenv environment_name Activating Virtual Environment workon environment_name 15 Django Virtual Environment Setup Listing Virtual Environment lsvirtualenv Removing Virtual Environment rmvirtualenv environment_name 16 Django Installation To install Django, first visit to django official site (https://www.djangoproject.com) and download django by clicking on the download section. Here, we will see various options to download The Django. Django requires pip to start installation. Pip is a package manager system which is used to install and manage packages written in python. 17 Django Installation The complete installation process is described below. Before installing make sure pip is installed in local system. Here, we are installing Django using pip, the installation command is given below. pip install django 18 Django Installation 19 Verify Django Installation After installing Django, we need to verify the installation. Open terminal and write python and press enter. It will display python shell where we can verify django installation. Look at the Django version displayed by the print method of the python. Well, Django is installed successfuly. Now, we can build Django web applications. 20 Verify Django Installation 21 Django Project To create a Django project, we can use the following command. projectname is the name of Django application. django- admin startproject projectname 22 Django Project Example Here, we are creating a project djangpapp in the current directory. django- admin startproject djangpapp 23 Locate into the Project Now, move to the project by changing the directory. The Directory can be changed by using the following command. cd djangpapp 24 Locate into the Project 25 Locate into the Project A Django project contains the following packages and files. The outer directory is just a container for the application. We can rename it further. manage.py: It is a command-line utility which allows us to interact with the project in various ways and also used to manage an application that we will see later on in this tutorial. 26 Locate into the Project A directory (djangpapp) located inside, is the actual application package name. Its name is the Python package name which we'll need to use to import module inside the application. __init__.py: It is an empty file that tells to the Python that this directory should be considered as a Python package. 27 Locate into the Project settings.py: This file is used to configure application settings such as database connection, static files linking etc. urls.py: This file contains the listed URLs of the application. In this file, we can mention the URLs and corresponding actions to perform the task and display the view. 28 Locate into the Project wsgi.py: It is an entry-point for WSGI-compatible web servers to serve Django project. Initially, this project is a default draft which contains all the required files and folders. 29 Running the Django Project Django project has a built-in development server which is used to run application instantly without any external web server. It means we don't need of Apache or another web server to run the application in development mode. To run the application, we can use the following command. python manage.py runserver 30 Running the Django Project 31 Running the Django Project Server has started and can be accessed at localhost with port 8000 32 Django MVT The MVT (Model View Template) is a software design pattern. It is a collection of three important components Model View and Template. The Model helps to handle database. It is a data access layer which handles the data. 33 Django MVT The Template is a presentation layer which handles User Interface part completely. The View is used to execute the business logic and interact with a model to carry data and renders a template. Although Django follows MVC pattern but maintains it’s own conventions. So, control is handled by the framework itself. 34 Django MVT There is no separate controller and complete application is based on Model View and Template. That’s why it is called MVT application. See the following graph that shows the MVT based control flow. 35 Django MVT 36 Django MVT Here, a user requests for a resource to the Django, Django works as a controller and check to the available resource in URL. If URL maps, a view is called that interact with model and template, it renders a template. Django responds back to the user and sends a template as a response. 37 Django Model In Django, a model is a class which is used to contain essential fields and methods. Each model class maps to a single table in the database. Django Model is a subclass of django.db.models.Model and each field of the model class represents a database field (column). Django provides us a database-abstraction API which allows us to create, retrieve, update and delete a record from the mapped table. Model is defined in Models.py file. This file can contain multiple models. 38 Django Model Let's see an example here, we are creating a model Employee which has two fields first_name and last_name. from django.db import models class Employee(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) 39 Django Model The first_name and last_name fields are specified as class attributes and each attribute maps to a database column. This model will create a table into the database that looks like below. CREATE TABLE appname_employee ( "id" INT NOT NULL PRIMARY KEY, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL ); The created table contains an auto-created id field. The name of the table is a combination of app name and model name that can be changed further. 40 41 42 43 44 45 46 47 Django Templates Django provides a convenient way to generate dynamic HTML pages by using its template system. A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted. 48 Why Django Template? In HTML file, we can't write python code because the code is only interpreted by python interpreter not the browser. We know that HTML is a static markup language, while Python is a dynamic programming language. Django template engine is used to separate the design from the python code and allows us to build dynamic web pages. 49 Django Template Configuration 50 Django Template Simple Example 51 52 53 Loading Template 54 Run Server 55 Django Template Language 56 57 Tags In a template, Tags provide arbitrary logic in the rendering process. For example, a tag can output content, serve as a control structure e.g. an "if" statement or a "for" loop, grab content from a database etc. Tags are surrounded by {% %} braces. For example. {% csrf_token %} {% if user.is_authenticated %} Hello, {{ user.username }}. {% endif %} 58 Django URL Mapping 59 Django URL Functions 60 61 Django Static Files Handling In a web application, apart from business logic and data handling, we also need to handle and manage static resources like CSS, JavaScript, images etc. It is important to manage these resources so that it does not affect our application performance. Django deals with it very efficiently and provides a convenient manner to use resources. The django.contrib.staticfiles module helps to manage them. 62 Django Static (CSS, JavaScript, images) Configuration 63 Django Image Loading Example 64 Output 65 Django Loading JavaScript //index.html Index {% load static %} 66 Django Loading CSS Example // index.html Index {% load static %} Welcome to Javatpoint 67

Use Quizgecko on...
Browser
Browser