Django…We all know the popularity of this Python framework. Django has become the first choice of developers to build their web applications. It is a free and open-source Python framework. Django can easily solve a lot of common development challenges. It allows you to build flexible and well-structured web applications.
A lot of common features of Django such as a built-in admin panel, ORM (object-relational mapping tool), Routing, and templating have made the task easier for developers. They do not require spending so much time implementing these things from scratch.
One of the most killer features of Django is the built-in Admin panel. With this feature, you can configure a lot of things such as an access control list, row-level permissions, actions, filters, orders, widgets, forms, extra URL helpers, etc.
Django ORM works with all major databases out of the box. It supports all the major SQL queries which you can use in your application. Templating engine of Django is also very, very flexible and powerful at the same time. Even though a lot of features are available in Django, developers still make a lot of mistakes while building an application. In this blog, we will discuss some common mistakes which you should avoid while building a Django application.
1. Using the Python Global Environment For Project Dependencies
Using the global environment for project dependencies can generate dependency conflicts. In Python, you can not use multiple package versions at the same time. It will create a problem if different projects require different incompatible versions of the same package. There are many options to isolate your environment. The most common ways are given below…
- Virtual Machines
2. Avoiding Pinning Project Dependencies in a ‘requirements.txt’ File
When you start with a Python project, start with an isolated environment with a ‘requirement.txt’ file. When you install packages through pip/easy_install, do not forget to add them to your ‘requirement.txt’ file. Later when you will have to deploy your project on the server, it will be easier for you.
Different versions of packages provide different modules, functions, or parameters. If there will be any minor change in your dependency then it can break your package. So it is important to pin the specific version of your dependencies in your ‘requirement.txt’ file. There are very nice tool pip-tools available in Python. With the help of command-line tools available in it, you can manage your dependencies easily.
This tool automatically generates a ‘requirement.txt’ file that pins all your dependencies and your entire dependency tree. Also, keep the backup of your dependencies file. Keep a copy in your file system, a Git managed folder, S3 folder, FTP, and SFTP.
3. Not understanding the benefits of both Function-based views and Class-based Views
Class-based views provide an abstract class implementing common web development tasks. You can get the advantage of using the structured API along with the advantages of object-oriented programming. Your code becomes more clear and readable. You can extend your CBVs for your view, and you can override the class properties or functions.
In your project, you can use different mixins, and you can override the basic CBV behaviors for building the view contexts, checking authorization on the row level, auto-building template paths from your project structure.
Function-based views are often a great option as well. Luke plant’s article does a good job of explaining why you might want to use FBVs: https://spookylukey.github.io/django-views-the-right-way/
4. Writing the Application Logic in Views Instead of Model
Writing the logic in views makes your application view “fat” and your model “skinny”. Avoid this mistake and always write the logic in your models instead of views. You can break the logic into small methods, and you can write that into the models. You can use it multiple times from multiple sources within just a few lines of code.
5. Messy and Unmanageable Setting File
A lot of times it happens that while working on a real-world project, your settings file grows to more than 600-700 lines of code. This huge and messy file becomes difficult to maintain especially when your dev, production, and staging environment requires custom configuration. You can divide the configuration file manually, and you can create custom loaders.
6. Bad Application Structure and Incorrect Resource Placement
Whenever you build an application with Django, it contains multiple apps. These apps are responsible for doing a specific task. Basically, these apps are Python packages that contain at least __init__.py and models.py files. In the latest Django version, you no longer require the Django version. __init__.py is enough in your application.
Your Django application is built on different Python modules such as models, admins, views, URLs, models, forms, template tags, etc. You can divide your application into reusable application logic.
Always give your project folder a specific name and place your application in project/apps/. After that, you can place your application dependencies into their own subfolders.
7. Confusion Between STATICFILES_DIRS and STATIC_ROOT in Django
In Django using the static management command python manage.py collectstatic, you can go through the STATICFILES_FINDERS and you can copy the files from static folders as well as from STATICFILES_DIRS to the directory you specify in the STATIC_ROOT setting.
We have mentioned seven mistakes in this article, but there are many things in Django, you need to take care of. While building a project in Django, follow the best practices to write code in your project. Everything matters, from defining a URL to creating a view or defining a model to the complete folder structure. It will be tough in the beginning but as you will progress you will see improvement in yourself. It’s okay to do these mistakes as a beginner but if you keep looking at the good Django project you will surely master in it.
Share your thoughts in the comments
Please Login to comment...