- GBlog, Python, Python Django

7 Mistakes You Should Avoid While Building a Django Application

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, templating have made the task easier for developers. They do not require spending so much time on 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, and 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 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 DependenciesUsing 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…virtualenvvirtualenvwrapperVirtual MachinesContainers2. 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 ‘requirment.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. Using Old Style Python Functions instead of Class-based ViewsIn Python, most of the time developers avoid using Class-based views due to the complexity of it. Using the Python function in an application’s views.py file might be a good idea for some time (for example: for tests or utility 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 CBV’s 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. 4. Writing the Application Logic in Views Instead of ModelWriting 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 FileA 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 PlacementWhenever 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 the 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 DjangoIn Django Static files mainly contains JavaScript, CSS, images, fonts, etc. They get collected into a public directory during the deployment process.  python manage.py runserver searches static files using the STATICFILES_FINDERS setting. If a failure occurs Django tries to find the file using django.contrib.staticfiles.finders.AppDirectoriesFinder. This look into the static folder of every installed application in the project. You can write reusable applications shipped with their own static files. 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. ConclusionWe 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.  Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course