Top 13 Mistakes That Django Developers Make

Publié le - Dernière modification le

Django is an open-source and free Python web that helps developers to solve challenges in web development, and in making well-structured and flexible applications. Mistakes like naming conflicts in static assets or maintaining large settings are common in development, and it is not only Django developers who are susceptible.

Django has numerous features such as the Object Relational Mapping (ORM) tool, Templating, Routing, and Admin. It has a unique admin interface feature which builds spontaneously from your admin panel models, and model’s schema. The admin interface helps users configure row-level permissions, extra URL helpers, filters, access control list (ACL), and widgets, and more. Django’s ORM works with numerous databases and supports every major SQL function and instruction. The templating engine of the program is powerful and flexible. You can access many tags and standard filters. Read through to see common mistakes made by Django developers.

1. Using Python environment global system for project dependencies

If you use the global environment for Python in your project dependencies, you will create conflicts. Python cannot work with compound package versions simultaneously. This is problematic if various projects demand incompatible versions of a similar package. Most of the new Django and Python developers do not know about the isolationist nature of the Python environment. There are various ways to isolate Python’s environment. Some include:

  • Virtualenwrapper: This is a Python package whose installation is global, and gives a toolset to delete, activate, or create simulated environments. Every virtual environment gets stored in a separate folder.

  • Virtualenv: This package creates a Python environment, and its scripts can activate or deactivate the packages in the environment.

  • Virtual Machines (VM): If you dedicate your entire virtual machine to an application, this is a risky isolation. You can use tools such as Parallels, VMware, Proxmox, and VirtualBox.

 2. Failure to pin dependencies in a requirements.txt file

You should begin all new Python projects in a new isolated environment, and a new requirements.txt file. A developer should ideally install packages via pip/easy install, but they should add them to the requirements.txt file - this way it becomes easy to deploy projects on servers, and a team member can bootstrap the project in their PC. The requirements.txt file should have explicit dependencies. Various versions have different function parameters, functions, and modules. A minute change can break the project, which you don’t want if your project is live. Make sure you back up your dependency files.

3. Failure to use class-based views and opting for old-style Python functionalities

A developer might choose a Python function for utility views or tests. However, expert developers recommend the use of class-based views (CBVs) in all applications.  CBVs provide abstract classes that implement common web tasks from professionals. Their API has an incredible structure, and you can apply all merits of object-oriented programming (OOP) if you use CBVs. Your code will be readable, and more clear. Do not use Django functions such as forms processing, CRUD operations, and listings. Choose the correct CBV for your project and ignore class features or properties which can change the view behavior. There are numerous projects on Freelancer which you can use to practice programming using Python.

4. Writing skinny models and fat views

If you don’t write your application logic in models and use views, this means you’ve written your code in a model based on the view. This makes the views “fat”, and the model “skinny.” Models should be fat and views should be skinny. Breaking logic into smaller models lets you use it numerous times from various sources. You will not have to copy-paste any code. You can also unit-test your code since the logic is in one place.

5. Using files with enormous and unmanageable settings

There are many settings in the new Django project settings file. A real project has over seven hundred configuration lines, which pose a challenge for maintenance. Change the configuration file by dividing it manually, and creating custom loaders.

6. Incorrect placement of resources and poor application of structure

Every Django project has multiple applications. According to Django notation, a Python package which has applications containing files in current Django versions is not enough. Django applications have Python modules such as management commands, static files, unit tests, database migrations, and Django specific modules (admin forms, URLs, template tags, views, etc.). Use simple logic to split your monolith applications into reusable applications. Describe what your app does in simple sentences, such as “allows users to sign up and leave a comment.” Name the project folder, and store all dependencies in separate folders.

7. Django developers confuse STATIC_ROOT and STATICFILES_DIR

Static files will not change as long as you are using the app. Such files include CSS, fonts, JavaScript, etc. Collect them in a public directory if you are using Django. Django uses the STATICFILES_FINDERS setting to search for all static files. You can write reusable applications and ship them in private static files. During production, you use a standalone server such as Nginx, which does not know anything about Django project applications. Django just gives the right static management command and copies every static file from various folders. The static data resources use the same logic with the Django development server. In your production environment, remember to run COLLECTSTATIC.

8. Default Django template loaders during production

If a developer uses the “assets never expire” policy, they can give users a pleasant experience which means the web will cache all static files. Users should download assets once - a few lines in Nginx can do this. What happens to cache annulment? If a user will download assets once, what will happen to JavaScript, font, and logo in the menu? To ensure this does not distract you, create unique filenames and URLs for all static files -  ManifestStaticFilesStorage is the best to do this. Note: the template folder you have cached does not reload on all models. Template parsing requires a lot of resources, and this is why developers parse all Django templates upon request, though this is bad practice during production.

9. Using python scripts for utilities

Django has an excellent feature in Management Commands which you can use for all your project services. You could also access Django Extensions to find an array of custom extensions. A developer could have worked on your commands!

10. Reinventing existing ideas

There are thousands of quick solutions in Python and Django. Check on the internet before you write something that already exists. You could find a platform where your input will help someone, while at the same time finding a solution you need.

11. Sticking to one favorite stack

You could have experience in a certain stack which makes you indispensable at work - but is this really helping your career? If your employer finds you are good in only one area, they could get rid of you in favour of multi-talented developers. Software engineering demands developers have multiple skills; a dedicated Python developer who has not tried to learn Java may find life difficult if their organization changes. Make sure you are proficient in one area,  and then move on to new stacks.

12. Ignoring soft skills

As you develop your career as a developer, focus on having soft skills such as mentorship, communication, and time management. Make sure you communicate with customers or colleagues at work, as it makes projects run more smoothly. Software development is a social activity, and if you don’t have soft skills you are only damaging your career.

13. Integrating heavy technologies with simple scripts

Many new developers do not understand that Django is not a massive tool. If you integrate your new code with tools such as JavaScript, you could be setting up your program for failure. It may not run if you do not exercise caution in your integration.

You will emerge as a good developer if you focus on improving yourself each day. Don’t concentrate on the mistakes you made yesterday, just learn from them and move on. Test yourself by starting a project from scratch, and always make sure you carry it through to completion.

If you’ve been a Django developer, you must have made mistakes. Share them in the comment section below!

 

Souscrivez maintenant pour des mises à jour hebdomadaires

Restez à jour avec les meilleurs articles de la communauté Freelancer. Souscrivez maintenant pour le meilleur contenu de Design, de Développement et de Freelancer chaque semaine.

Publié 22 août, 2017

LucyKarinsky

Software Developer

Lucy is the Development & Programming Correspondent for Freelancer.com. She is currently based in Sydney.

Article suivant

Code Optimization: The Complete Guide