
Let's help developers figure out if the Django framework is right for their next project. It is likely - suitable.
You should not grab a specific programming language or framework just because you used it in your previous project, or simply because you are familiar with it. So things are not done.
Before embarking on a new project, you should evaluate which language or framework is best for you to achieve the desired result. What is most important to you? Safety, development speed, scalability, versatility, support?
It is better to make an informed decision before starting to work than to repent of being hasty (or, worse, to put crutches on the project during the implementation process, because we did not bother to support it).
')
I have worked with different technologies for many years, dealt with both mobile and web development, and I believe that Django offers such a complete set of features that are not found in any other web framework.
I understand this is a loud statement. Let me justify it.
“Django is based on many sites that are used in the most active way, in particular, Instagram and Pinterest. Even Facebook uses Django for many of its utilities. Django originated in the publishing environment, so it’s not surprising that this framework is used on sites like The Washington Post and Smithsonian Magazine. ”- Amit Ashvini , Vice President of Marketing @ Zibtek
Overall look: when to use Django
If at least some of the following theses are about you (and there are no abstracts in the list with which you strongly disagree), then it is quite likely that Django is well suited for your project.
- You need to develop a web application or server-side API.
- You need to work quickly, quickly deploy and make changes to the project as you go.
- By default, the application should be protected from the most common vulnerabilities and attacks, in particular: CSRF, SQL injection, XSS, clickjacking, etc.
- At any time in the application may require scaling: both capacity, and reduction.
- In the future, you plan to integrate the latest technologies, for example, machine learning.
- You need to use a reliable framework that is being actively developed, used by many top companies and leading websites all over the world.
- It is required that both the web application and the server part of the API are in the same code base, consistent with the “single source of truth” (DRY principle)
- You do not want to work directly with database queries, and you need ORM support.
- You are going to use free software.
- If you get stuck, you will have to look for a solution yourself, so you need good documentation and a responsive developer community.
In addition to the above factors, you need to consider what skills you (or your team) possess.
If you are a web developer and you already know how the web works, then working with Django will go relatively smoothly for you. It is necessary to understand how Django is structured, and some other things, of course, also - and consider that you are ready.
Sites running the Django framework
The history of Django has been around for about 10 years. During this period, it was used in production on a variety of top sites. Here are some outstanding examples:
Pinterest EngineeringMozillaBitbucketUdemyThe onionDisqusWashington PostNASASpotifyInstagram EngineeringNational geographicThe guardianJsfiddleDo you still doubt whether to spend your precious time to practice with Django? To begin, let's discuss the reasons why Django CAN NOT COME for your project:
When not to use Django
- You are dealing with a huge application, and it simply does not fit in the same code base. It may be better to break your application into microservices. With each of its levels a specially selected team will cope better. Other technologies will be suitable for each specific use case. In some such scenarios, Django may be useful, but it would be impractical to fully develop such an application on Django (as well as on any other separate framework).
- You need to write a simple application, in which you do not need to work with the database, perform file operations or do something a little more complicated.
For such situations microframes are better suited. One of the most popular microfreemorkov - Flask, like Django, it is written in Python. Similar microframes are available in other technologies, for example. Slim in PHP, Apache Spark in Java, Express.js in Node.js, etc. - You want to write everything from scratch yourself and know what you are doing.
- You or your colleagues are completely unfamiliar with Django / Python, and you do not have the time and resources to develop the necessary skills.
The best solution in the latter case is to work with what you best understand. If you take on a new technology or framework, the chances of screwing up increase many times over.
If all of the above is not about your project, then it is likely that Django will suit you.
Reasons to use Django
The Django framework is written in Python:
I know you know that.
Therefore, I will take this opportunity to highlight some of the key advantages of Django, which he inherited from Python. I'll be brief.
Python is one of the most popular and fastest growing programming languages ​​in the world.
sources:
TIOBE indexIndeed.com Jobs' data analysis by Coding DojoGithub octoverseLearning Python is really very simple. Usually, modern developers are the first to learn this particular language.
The above does not mean that this language is only for beginners. Python is also used in state-of-the-art technologies. Python is actively used in the technological stack of many giant companies, including Google.
Python is great for developing web scraping tools.
It interacts well with other languages.
Developing in Python does not mean that you will be forced to write anything and everything only in Python.
You can use libraries for many other languages, including C / C ++ / Java.
Python ported, easy to read.
Python can even run on a JVM. Get to
know jython .
Python is widely used in popular technologies such as Big Data and Machine Learning.
You get access to a huge PyPI library.
All Inclusive Django
“All inclusive” means that Django is out of the box equipped with most of the libraries and tools needed in common practical situations. Listing: Django ORM, middleware, authentication, HTTP libraries, multisite support, i18n, Django Admin, templating engine, etc. - And that is not all". No other framework I know provides such broad support right away.
Some consider such a circumstance a “minus”, and others - a “plus”. Each party is right in its own way, and I somewhat agree with both.
This is a minus, because in such a situation the framework turns into a monolith.
I believe that if you need these capabilities that lead to the formation of a monolith, then you somehow have to use some other library (or write it yourself).
Why, then, do not use the tool in which all this already exists, tested in battles, functions on the largest sites, is actively developed and provided with community support?
If you do not need most of the features offered in Django, then it is better to stop at some microfragm.
Do not reinvent the wheel - do you remember? Spend your time on what is really important, and let Django do the rest.
Django admin
Although I mentioned this element in the previous section, it deserves closer attention. In many frameworks, in particular, Laravel, Yii, etc., attempts were made to simplify the work with the admin panel. I’ve been able to develop many projects in different frameworks, but none of them even compares closely with Django for the convenience of working with the admin panel.
Some people think that Django Admin is not flexible enough, and it takes a lot of effort to adjust any part of it to fit your needs. At the beginning of working with Django, I was inclined to agree with this, but over time, having understood the framework, I became disaccustomed to this. Yes, it has its own learning curve, but not a second that you give it will not be wasted.
In fact, Django Admin is very well structured. In some of my projects I used the Django admin panel “as is”, and in others I completely replaced it with my own templates, which I developed from scratch. In any case, it took no more time than developing with any other framework I know of.
The main plus? You get out of the box access rights and authentication. It would take weeks (or at least a few days) to develop all this from scratch.
DRY Principle (Do Not Repeat)
I know many frameworks whose supporters claim that they really correspond to the principle of “DRY”. I have worked with many such frameworks, but in none of them the “DRY” principle is implemented as it should.
Unfortunately, in most frameworks, the “DRY” principle is simply not given enough attention. In my opinion, if you are writing an application that you are going to regularly update (and this can be said about most modern applications), then you should follow the DRY principle to avoid problems.
So, in Laravel you have to write a validation for each procedure separately. The situation is the same with most other frameworks. To make your code comply with the DRY principle, you need to work hard. It's hard to keep track of, especially if you work in a team.
In turn, the Django framework is designed in such a way that to violate the principle of DRY there usually comes only on purpose.
So it should not be, right? Consider an example.
This is how database validation and migration is done in Django.
Create a
Model
class with the required fields. We specify all additional validations and restrictions we need.
Migrations are generated by a single CLI command: `python manage.py makemigrations`.
Changes are made to the database by a single CLI command: `python manage.py migrate`.
Validations and restrictions are automatically checked for each CRUD operation - whether it is Django Admin or Django REST Framework. You do not have to write validation again.
The same model class is used to generate Django Admin CRUD views. You do not need to add any custom HTML / CSS.
Compare these conditions with any other framework - and I think you would never be able to do anything like this in just the next few lines of code:
class Employee(models.Model): name = models.CharField(max_length=127) email = models.EmailField(null=True, blank=True) created_at = models.DateTimeField(blank=True, null=True, auto_now_add=True) updated_at = models.DateTimeField(blank=True, null=True, auto_now=True)
This is not only about “not repeating”. This approach saves you from bugs in the future. We all found ourselves in situations where it was possible to change something in one place, and forgot to replace it in another place - and it turned out only after a lot of users started having problems.
In Django, returning to the above code, if you ever have to replace the
`max_length`
field with something else - just do it here. The change will automatically apply to the validation of all routes and to the database.
Object-relational mapping in Django
Django provides a full-featured ORM mechanism out of the box.
I worked with a variety of ORM tools in various technologies, including Eloquent, greenDAO, Yii AR, etc. In all of them, the simplest requests are handled fairly well, but sooner or later I had to write certain requests from scratch, because the ORM mechanism could not cope with a specific practical case.
With Django ORM, I haven’t been in such situations. It works so well that you can just forget that you are working with database queries. This is exactly what object-relational mapping should be. Below are some examples of Django ORM:
# 5 , rank = 10 age <= 30 top_young_employees = Employee.objects.filter(rank=10, age__lte=30)[:5] # employee = Employee.objects.create(name='John Doe', age=35, country='IN') # print(employee.name)
Rapid development
The creators of almost any web framework love to boast about it, and, perhaps, they are all really right - depending on what meaning we put into the word “swift”.
True, with Django, some things are done hilariously fast. You have already seen how easily we were able to determine the admin UI, database table and perform validation.
It was just the tip of the iceberg.
In principle, rapid development is not a feature as such, but only an organic consequence of those present in Django DRY, ORM, the template engine and the all-inclusive philosophy.
Django framework security
Let's admit, sometimes developers are lazy. I am so sure. From time to time I procrastinate, postponing the decision of critical tasks. This is where various vulnerabilities may arise.
I especially like the fact that Django does not go for security cuts to speed up the pace of development. Security features are activated by default, so it doesn’t matter whether you are lazy or not.
Open source, excellent documentation, a huge community, etc.
Since Django is an open source and extremely popular framework, a responsive community has formed around it. I think you know what the merits of free software are - and so, they are all inherent in Django.
Django official documentation is more than enough for any developer. If you get stuck - to find a solution is not difficult.
You may already have the impression that Django has created many of its own libraries, so you may be surprised that the special library for testing has not been done here. No, do not think that the Django framework does not support testing - it supports, as well. Simply, following the principle of "Do not repeat" it would be pointless to develop a library for testing, when there is already an excellent library of this kind in Python itself. Django interacts well with it. In addition, it is very well combined with third-party libraries, for example, pytest.
Modern state of Django and other popular frameworks
So, I tried to highlight the problems I encountered when working with other frameworks and compare these frameworks with Django. Having worked with Yii, CodeIgniter, WordPress, CS-Cart, Laravel, etc., I came to the conclusion that Django is much better than any of them.
However, this is just my opinion.
If you like statistics, here is the annual study Stack Overflow, where Django figures among the most popular and sought-after frameworks:
Frameworks, Libraries, and ToolsMost Loved, Dreaded, and Wanted Frameworks, Libraries, and ToolsIn addition to the above experience with PHP, I also developed Java applications for Android, React.js client applications. In all these cases, I spent a fair amount of time refactoring the code base, looking for the best architecture, getting bogged down in problems with scalability in a couple of months and again starting to refactor.
I recently rewrote one application from Laravel to Django that I have had in production for over a year. I managed to deploy a new code base in less than 10 days, having written for this the minimum amount of code (I say: the complexity decreases!) In the opposite direction, such an operation would definitely take more than a month.
If you try to directly compare other frameworks with Django, it will not give you anything.
Performance monitoring can show that the Java framework is faster than Django. You can be well versed in PHP, so perhaps developing an application on Django will go faster with you than on a familiar PHP framework. In the case of a very simple application, setting up Django may seem a bit tedious - of course, it is much easier to write a file with scripts. Survey results can vary depending on the audience among which they were conducted.
However, here we talk not only about frameworks related to other technologies. Even if you are familiar with Python, it is possible that the Flask microform will seem more convenient and desirable to you. We'll have to think about which of them to stop.
My advice is simply do not compare them.
Conclusion
In my opinion, Django managed to perfectly balance performance, architecture, developmental complexity, security, and scalability.
If you start writing a project from scratch, I strongly recommend trying to do it with Django.