Introduction to Django

Learn via video courses
Topics Covered

Overview

Django is a high-level Python web framework that allows the rapid development of secure and maintainable websites. It is free and open source. It is used for full-stack app development and server development. Django is suitable for the backend as well as the frontend. It is a collection of Python libraries that allows us to develop useful web apps ideal for backend and frontend purposes.

Introduction to Django

Django

The Internet has become a crucial part of our lives, and so are websites. Django along with Python helps us to develop dynamic websites. Now you must be wondering what a dynamic website is, well it is a website whose contents are changed from time to time. Well, what is Django? Django is a high-level Python web framework used for full-stack app development and server development. With the features like a login system, database connection, and CRUD operation, Django makes the creation of an app easier. Django handles much of the tedium of web development, allowing you to focus on writing your app. Django is an Open Source Framework, the GitHub Repository of Django receives more than 2000 contributions every month. Its wide adaptiveness and open-source nature make it a continuously evolving framework. Developers struck with a problem can easily find solutions due to its active community on the Internet.

Why Django?

Listed below are the reasons to use Django:

  • Easy to maintain: Django code is easy to use and maintain. It is easy to install and run.
  • Secure: It offers codes and enables protection against vulnerabilities related to security.
  • Versatile: Any type of website can be developed using Django. For example, news sites, e-commerce sites, social networking sites, and so on.
  • Scalability: It's loaded with extras and scalable, allowing you to create applications that handle high traffic and large amounts of data.
  • Built-in admin interface: Django includes a default admin interface that can be used to directly create, read, update, and delete models.
  • Easy to switch databases: Django allows implementing multiple databases such as PostgreSQL, MariaDB, MySQL, Oracle, and SQLite in projects.
  • Portable: The websites developed in it can run on any platform like Linux or Mac OS.

Why Django

Features of Django

Some of the features of Django are:

  • SEO optimized: Search Engine Optimization is adding websites to a search engine so that it appears in the top results. Search engines use some algorithms which sometimes don’t cooperate much with the web developer. We are creating our website in the human understandable form and they have to add it in the URL form on the server so that it is recognized by the search engine. Since Django handles the website through URLs and not the IP address of the server, it makes it easier for the engineer to add the website to the server and also doesn’t have to convert the URL into numeric code.

  • High scalability: Django does not use any other Python library other than the one created by the developers themselves. That makes Django ideal for anyone to make error-free scalable websites.

  • Secure : Django prefers security and assists developers in avoiding many common security flaws such as SQL injection, cross-site scripting, etc. Its user authentication system allows for the secure management of user accounts and passwords.

  • Versatile : Django's versatility allows it to build applications for a wide range of domains. Companies are now using Django to build a variety of applications such as content management systems, social networking sites, and scientific computing platforms.

  • Portable : Django is a portable framework, and its code can run on any platform, including PC, Mac, Windows, and Linux. This framework's cross-platform nature enables developers to support all development and production environments.

  • Rapid development: This framework makes the application development process easier and faster without writing the code from scratch. We are not needed to make separate server files to design the database and connect the same while also making another file for transferring data to and from the server. Django handles this work and some other tasks as well. Django provides comfort and makes the development of complex and data-driven websites much easier.

History of Django

Django was developed in 2003 by the web developers of Lawrence Journal World Newspaper, Arian Holovaty, and Simon Willison. Adrian Holovaty named this web framework after a famous jazz guitarist, Django Reinhardt. It was publicly released in July 2005 under the BSD license. Currently, DSF (Django Software Foundation) is maintaining its development and release cycle.

Django is a very well-known framework. The Washington Post handles traffic every minute using Django. Some other renowned news apps and social apps using Django as their helping hands are The New York Times, The Onion, The Guardian, Instagram, and Youtube. Apart from them some other educational websites like National Geographic, NASA, etc.

How does Django Work?

Django follows the model view template architectural pattern:

  • Model: It is defined as the structure of stored data, including the field types, their maximum size, default values, etc. Each model maps to a single database table.
  • Template: It deals with the presentation of data.
  • View: It is a component that includes the logic that will be displayed to the user, usually represented by HTML, CSS, or Javascript files.

What does Django Code Look Like?

Well, you must be wondering what a Django code looks like. Let us take an example and create a project named Online Shop with apps Products, Customers, and Shopping Cart. The codes for Django are written in the Command prompt or Windows Powershell, right after we are done installing it in our system.

  • At first, we are entering a virtual environment using virtualenv venv and activating it using venv\scripts\activate in the first two lines of the code.
  • After activating the virtual environment, we are starting the project using the code django-admin startproject OnlineShop.
  • Lastly, start the app with, python manage.py startapp Products

Model

Model in Django is the way the data is stored. For example in the model.py under the products app, we will be dealing with the components of the products like product name, product price, and quantity. Django uses ORM(Object Relational Mapping) to link code to data. So, Django allows us to interact with its database models, i.e. insert, modify, remove, etc. using an API called ORM. It is used to interact with data from various relational databases such as SQLite, PostgreSQL, and MySQL. Generally, each model maps to a single database table. Each model is a Python class that is a subclass of django.db.models.Model .

Refer to the example section for more explanation.

View

View acts as a link between the model and templates. It receives the user request, retrieves the appropriate data from the database, then render back the template along with the retrieved data. We write view functions in the views.py file. In this function, we link each HTML file. We call a particular view using the URLs in the urlpatterns present in the urls.py. Kindly refer to the example section.

URL Routing

It links URLs and displays templates. In the urls.py file of the inner project folder, it imports the view function from views.py to the urls.py.

Template

A folder named template must be created in the outer project folder, and all the HTML files are stored in it. In the settings.py file, present inside the inner project folder, the name of the folder template is added in the DIRS setting under the TEMPLATES.

Examples

  • Sending the request to the right view in the urls.py file under the inner project file : In this code using path('first/',views.first) will render views.first function from the views.py file. For path localhost:8000/first/ , it will look for views.first view function, from there under return render(request,'page1.html') in the views.py file, it will look for page1.html in the template folder which we created to store the HTML files.
  • Handling the request in the views.py file : In this code, we are creating a view function def first(request): to link the HTML page,page1.HTML with the function named "first" we created.
  • Defining data models in models.py : Models.py file is present inside the folder of each app. Under this code, we are linking the data of the Product app by taking the name of the product as the character, price as a float value, and quantity as an integer.

Conclusion

Hello developer! I am sure by now you must have got an idea about what is Django. Let us summarize what we learned from this article

  • Django is a Python web framework for developing applications.

  • It is secure, versatile, portable, and easy to maintain.

  • It follows the MVT(model, template, view) architectural pattern.