Among the most widely used back-end frameworks we find Django: a popular Python framework for building applications. Promising easier development while guaranteeing fewer lines of code to write, developers favour the framework that will save them time without costing them quality.
An introduction to Django
Django is an open-source back-end framework. It is developed in the Python programming language. Created in 2003 by the local newspaper of the city of Lawrence in the United States, the framework moved to a BSD licence in 2005.
Today, Django is very popular; it is one of the most widely used back-end frameworks and has more than 2,200 contributors. Several social networks have chosen to use Django, such as Instagram and Pinterest.
Getting to grips with the framework is fairly straightforward. The documentation provided is rich and detailed, and the community is very active. In addition, developers have access to a forum set up by Django in order to encourage mutual support within the community. All of this allows developers who have never used the framework to take their first steps and start a project easily and independently. The only prerequisite: having the basics of Python!
Quick installation
The back-end framework can be installed in one of 2 ways:
- A quick installation
- An installation with a server
The quick installation gives you a first look at Django so you can try it out. The framework includes a web server in order to avoid Apache configuration. However, with this installation it will not be possible to deploy the code to production.
First of all, you need to set up the virtual environment using the command python -m venv env and then activate it with the command source env/bin/activate. As Django is a Python framework, you need to use the pip package manager to install it: pip install django. It is now possible to create the project using django-admin startproject projectname. This will automatically install the various files needed to create a base code, that is, the minimum files required for the Django project to be functional. All that is left is to run the development server using the command python manage.py runserver and pick up the http address that is returned.
Installation with a server will allow the code to be deployed to production. To do that, you first need to install and configure Apache with the mod_wsgi extension. The rest of the installation remains the same.
Whichever method is used to install Django, you need to check that Python is already installed on the machine. Django is currently at version 4.0.5. This version is compatible with Python from version 3.8 of the language onwards.
One point to note: if several versions of Python are installed on the machine, you will need to specify which version to use in every command that calls Python.
The framework’s guiding principle
The framework promises fast, easy development with little code to write. But less code does not mean less quality for the framework!
Indeed, clean, quality code will be far more effective on several levels. First of all, it allows developers to find their way around the lines written more easily. But this quality also makes for better ranking with Google, which appreciates not having its time wasted.
So less code simply means that Django takes care of certain features in order to leave developers free to customise the application according to requirements. These features handled by Django may include database manipulation, URL routing or dealing with vulnerabilities and security.
Security
Django places major emphasis on the security of applications developed with the framework. It is keen to help developers avoid certain mistakes and certain vulnerabilities.
In its documentation, Django gives plenty of advice and good practice for avoiding code vulnerabilities. For example, Django protects the database from SQL injection because its queries are built by separating the parameters from the condition.
URL routing
Still with a view to developing web applications with quality code, Django encourages developers to configure clean URLs.
To do this, you need to create a Python module that will be used as a router, containing the defined URL and the view used.
MVC architecture
MVC stands for Model View Controller; it is a software architecture pattern. Widely used in back-end frameworks.
The pattern separates the 3 entities:
- The controller acts as the intermediary between the model and the view. It checks and manipulates the data. This is what is known as business logic.
- The model contains the data and returns it to the controller.
- The view creates the graphical interface with the data sent by the controller in order to pass it on to the user.
Separating the 3 entities makes the application easier to maintain because it is possible to change the view without having to touch the controller.
Likewise, separating the entities encourages teamwork, since a front-end developer will be able to work on the view at the same time as a back-end developer works on the controller.
Django is a mature framework that has built up an active community. Designed for developers who are short of time, the framework helps maintain security and code quality. Not very complex to get to grips with, Python developers appreciate it for its versatility, which allows them to create all kinds of applications.
For more articles, take a look at our blog.