Introduction
Django is a popular Web framework written in Python.
Before learning more about Django, let’s understand Web framework first. Web framework is a software framework that is designed to support the build and deploy dynamic web sites, web applications, and web services.
A Web framework consists of a set of functionalities and tools that makes Web development easier for developers by solving some of the common issues, such as database access, security features, template processing, sessions, localization, URL routing and many more.
Using a Web framework, a developer can build a secure and reliable Web application very quickly in a standardized way.
What is Django?
Django is a high-level Python web framework that enables rapid development of websites. It is free and open source.
Initially released in 2005, Django 2.0 was released in Dec 2017 and Django 2.1 in June 2018 with a smorgasbord of new features.
As per the official website, Django is
- Fast: Django was designed to help developers take applications from concept to completion as quickly as possible.
- Secure: Django takes security seriously and helps developers avoid many common security mistakes.
- Scalable: Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.
The primary goal of Django is to ease and accelerate the development of complex, database-driven websites. The framework has been designed to emphasize the reusability and “pluggability” of components, low coupling, less code, rapid development, and much more.
In Django, Python is used throughout, even for settings files and data models. It follows the Model-View-Template (MVT) architecture and is generally used for building complex and data-driven websites.
So, what’s so special about Django?
For starters, it’s a Python Web framework, which means developers can benefit from a vast collection of open source libraries out there. Python is also one of the main reasons people started learning Django.
Django is a widely used Web framework written in Python. It’s one of the most popular Web frameworks – offering an array of features, such as a standalone Web server for development and testing, middleware system, caching, template engine, ORM, form processing, interface with Python’s unit testing tools.
Django also comes with battery included, offering built-in applications such as an administrative interface with automatically generated pages for CRUD operations, an authentication system, sitemaps, generation of syndication feeds (RSS/Atom). It also has a Geographic Information System (GIS) framework built within.
Django officially supports four database backends:
- PostgreSQL
- MySQL
- SQLite, and
- Oracle
Django can be run in conjunction with Apache, Gunicorn, Nginx using WSGI, or Cherokee using flup (a Python module). The framework also has the capability to launch a FastCGI server, allowing use behind any web server which supports FastCGI, such as Hiawatha or Lighttpd.
Other features available in Django along with aforesaid are:
- SEO Optimised: This is a special feature of Django due to which it has edge over others.
- High Scalability: A lot of MNCs on a worldwide scale uses Django and it gets implement there without any defects or errors.
- Versatile in Nature: Django is very versatile in its own Django way. The logical project structure and MVT architecture of Django sometimes seem very limiting.
Who’s Using Django?
Some of the biggest Web sites using Django are Instagram, Mozilla, Disqus, Bitbucket, National Geographic, Nextdoor, The Washington Times, Last.fm.
You can see the Django Sites database for more examples, they have a long list of Django-powered Web sites.
According to the stats provided by BuiltWith, more than 24,194 websites are using Django Language.
Create the First Django App
You don’t need special tools for developing a Django project, as the source code can be edited using any conventional text editor.
Source: visualstudio
- In the VS Code Terminal with your virtual environment activated, run the administrative utility’s startapp command in your project folder (where py resides):
python manage.py startapp hello
The command creates a folder called hello that contains a number of code files and one subfolder. Of these, you frequently work with views.py (that contains the functions that define pages in your web app) and models.py (that contains classes defining your data objects). The migrations folder is used by Django’s administrative utility to manage database versions as discussed later in this tutorial. There are also the files apps.py (app configuration), admin.py (for creating an administrative interface), and tests.py (for unit tests), which are not covered here.
- Modify hello/views.py to match the following code, which creates a single view for the app’s home page:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
- Create a file, hello/urls.py, with the contents below. The py file is where you specify patterns to route different URLs to their appropriate views. The code below contains one route to map root URL of the app (“”) to the view.home function that you just added to hello/views.py:
from django.urls import path
from hello import views
urlpatterns = [
path("", views.home, name="home"),
]
- The web_project folder also contains a py file, which is where URL routing is actually handled. Open web_project/urls.py and modify it to match the following code (you can retain the instructive comments if you like). This code pulls in the app’s hello/urls.py using django.urls.include, which keeps the app’s routes contained within the app. This separation is helpful when a project contains multiple apps.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", include("hello.urls")),
]
- Save all modified files with Ctrl+K S.
- In the VS Code Terminal, again with the virtual environment activated, run the development server with python manage.py runserver and open a browser to http://127.0.0.1:8000/ to see a page that renders “Hello, Django”.
If you want to build a Python-based application with Django, get in touch with our team. We will help you build the best application for you and your business.