Python is a versatile programming language with a simple syntax and a huge community. With it, you can solve tasks of various sizes — from creating bots, to developing applications and websites, and even machine learning.
Many frameworks and libraries have been developed in Python with ready-made modules and code packages. Programmers use them to reduce development time. In this article, we’ll be talking about the Django framework.
Python framework Django
What tasks we solve
- Creation of data processing services
- Creation of web applications, social networks, and web chats
- Development of various APIs
- Creation of CMS, CRM, and email services
Types of companies it’s suitable for
E-commerce, learning platforms, think tanks, information and news resources, mailing management services, social networks, and startups
Fast development, the ability to easily make changes to the functionality of systems and scale projects, a high level of security, and the stability of services
Why Choose Django
Django is a popular and multifunctional framework for web applications, as well as a set of libraries: components, modules, utilities, and ready-made solutions in the Python language. Pinterest, Instagram, Dropbox, Bitbucket, Mozilla, and Discovery have all been developed on Django.
Django is best suited for back-end development of projects of medium complexity.
For example, if you need to implement a website, web application, or data processing service. With the help of built-in and additional libraries, the framework allows you to quickly add necessary functions: connect a database, create an API, install a web chat, or implement an email newsletter.
You can create a complete website with Django. The built-in template engine and methods for generating html pages allow you to develop front-end for simple sites. For medium and large projects with dynamic content, Django’s capabilities are insufficient, so this framework is used as a backend, and the frontend is developed based on React, Angular, or Vue.
The main advantage of Django is fast development. The framework doesn’t require a lot of time to set up the configuration, and it allows the programmer to quickly create an MVP — a test version of the product with a basic set of functions. This is necessary for testing of an idea before big money is invested in it. More in the article: «MVP: How to Test a Business Hypothesis and Get Funding».
If the customer and the target audience like the implementation, the project can be finalized in accordance with the detailed terms of reference. To do this, you need to make changes to the code and register the necessary settings.
Tasks we use Django for in projects
Develop an API
On Django, you can implement an API — a data exchange mechanism. Applications, websites, and other services constantly request and send information to each other through this protocol. Thanks to the API, sellers make online transactions, exchanges show current exchange rates, and ticket aggregators show data from airline websites.
It works this way: as an example, a user looks at a product card in an online store and wants to know how many pieces are left. A request is sent to the server. The API is needed so that the system correctly understands the request, and in response sends the necessary data from the server: stock balances from a database. Then the user will see on the site how many units of the product he is able to purchase.
Depending on the tasks required for the project, developers implement the API in the REST, GraphQL, gRPC, or Websocket API format. Django can implement any of these API types:
- The most common request-response communication method between a client and a server is REST. The Django-rest-framework library makes it easy to implement a Django-based REST API.
- Sometimes REST doesn’t allow you to receive data in a usable form. In this case, you can implement the API using GraphQL. This method is suitable if you need to make a selection in one answer and clarify which data is needed.
- gRPC and Websocket API can reduce network traffic load. Websocket also allows you to receive real-time updates. These protocols are used less often because their development is more complex. They are used where it’s justified by the tasks required.
Connect third party services
You may need to link the application to other services. For example, add data storage in the cloud, user authorization through social networks, processing uploaded videos, or sending reports to email.
In Django, this is done quickly. You just need to connect the necessary extensions — additional libraries.
Databases are an integral part of almost any Internet service. They store data about users, products, information about orders, pictures, videos, etc. Django supports the popular SQL databases MySQL, Postgres, Oracle, and MariaDB.
For the application to work with the database, the programmer describes the parameters for connecting to it in the configuration file and installs the necessary adapter, i.e., a library that allows the application to interact with the database.
Sometimes project requirements change, and you need to make changes to the database structure to perform migrations, for example, add new tables or remove unused fields. Migrations allow you to change the current state of databases to meet new requirements, or return modifications to their original state.
Django provides convenient management of migrations. It usually suffices to make changes to the model and run the «makemigrations» command. The migration is created automatically in a special file, and can be applied with the migrate command.
Ensuring data security
To ensure data security, Django provides a set of built-in Middleware components. You can also include additional libraries. These tools allow you to set access rights and protect users' personal data and other information on web resources from attacks. For example, using the django-cors-headers library, you can allow access to a resource only from certain domains.
When should you use Django?
- A medium-sized project with a monolithic architecture is just right for Django.
- If there is no clear technical task, you can build a prototype and release an MVP, and then add details to the project.
- When fast development is required. It’s ready-made framework solutions allow you to concentrate on completing the task, and not be distracted by developing unnecessary code.
- If you need to create a CMS, CRM, or email-mailing service.
- When you connect third-party services to the project: social networks, cloud storage, etc.
- To set up interaction with a database. Django ORM is a convenient functional tool for working with databases.
- To implement APIs. Out-of-the-box templates and methods for creating different kinds of APIs make it fast.
Projects Django is not suitable for:
- If the project is very small or databases are not used. Django is a Python monolith. For simple applications it has a lot of superfluous things, such as components for working with statics, a built-in admin panel, and functionality for rendering templates.
- If low-level customization or a specific project architecture is required, Django’s capabilities are insufficient for this.
A practical example from OrbitSoft
We were contacted by the owner of a platform for organizing distance learning. Client companies place their training courses, tests, webinars on the platform. Users take these courses and complete assignments to test their knowledge. The data received needed to be processed and displayed in the personal accounts of users. But the platform wasn’t able to store statistics and calculate the results of completed tasks.
We developed a separate data processing service. It receives information about each user from the platform server, sums up the scores for all completed tasks, and sends the results to the platform client server. For this we:
- Connected the REST API to receive data from the learning platform
- Set up processing of received data
- Connected a database to store intermediate results
- Implemented background execution of pending tasks through Celery, a library for executing tasks from queues
- Connected a bot to Telegram to send error messages when performing tasks