•   Lab AI
  • Mar 8, 2019
  • Tags Python Django

7 Tips and more to ensure a successful Django project

Tools and tips that Django developers should know

Given the success of Python as a programming language, and its applications with AI and machine learning, many companies are opting to use Python for their web platforms. Python’s most popular and powerful web framework “Django” have been making Python developers’ life easier and faster due to it’s wide range of libraries, security, scalability and extensive documentation. 

The so-called “web framework for perfectionist with deadlines” is surely living up to its name .

While the many integrations and possibilities involving the world of Django can be overwhelming, here are some tips to make your Django development as easy as possible. 


Tip # 1:  Follow Django philology 

Django is oriented to agile and fast development. It aims for developers to create less code and a higher consistence among the programming layers. Whenever you can: follow the Django philosophy, this will ensure that you are following good practices.  

python zen

In case you are not familiar with Django’s philosophy, you can check it out here: https://docs.djangoproject.com/en/2.1/misc/design-philosophies/


Zen of Python: https://www.python.org/dev/peps/pep-0020/ 


Tip # 2: Use existing apps 


Don't reinvent the wheel: use third parties libraries. Chances are that if you are facing a common problem some else already solved it first.  First look for the django official releases or a third party libraries. This will save you time and will ensure some safety that the libraries have been tested by multiple Django developers. 


Django packages: https://djangopackages.org/ 


Tip # 3: Create a virtual environment for each project. 

This makes it easier to deploy and easier to replicate the environment into different machines and servers.  


Install virtualenvwrapper (python3): 

pip3 install virtualenvwrapper


Create new virtual env:

mkvirtualenv <project name>


To use the virtual environment

source activate

To stop:

deactivate



Since each python project has its own dependency libraries, having a virtual environment will make it safer and smoother to deploy. You can install the libraries in the visual environment using pip command. 


Additionally you can use the command:

pip3 freeze > requirements.txt

And later pip3 install -r requirements.txt


Following the django philosophy, you should keep a skeleton of the project using the logic: 

Project -> web page 

We should try to create 1 directory per each app functionality. 


Tip # 4: Apps recommendations 

Many small apps are better than giant apps. This is aiming for reutilization of apps and code

You should be able to explain the functionality of an app in one sentence, if it’s not enough then divide into different apps. 

Regarding app naming: Keep it short, try to use single words for your apps


Tip # 5: Divide your setting file by environments 

Create a base setting file and keep each environment inside a folder. For example: 

base.py (or  settings.py)


# along with the other settings.py variables ...

def _add_installed_app(app_name):
global INSTALLED_APPS

installed_apps = list(INSTALLED_APPS)
installed_apps.append(app_name)
INSTALLED_APPS = tuple(installed_apps)

ADD_INSTALLED_APP = _add_installed_app

development.py 

from base import *

ADD_INSTALLED_APP('debug_toolbar')

production.py

from base import *



Tip # 6: Keep your views as small as possible

Django is a model-template-view framework. Avoid adding any logic inside the html templates. Try to follow: Fat models thin views. 

Try to keep the views to the minimum code. The views should only have few logical or validation code. Put all your logic, validations, processes inside the models. The presentation layer should be only for User Interface. It will make it easier to maintain and more organized.

Keep your templates maintainable. 

Django HTML templates are limited, if there is a strong reason why you would need to add some logic to the views use JINJA2. Treat this as an exception and use jinja2 only for those views. 

More about jinja: http://jinja.pocoo.org/docs/2.10/switching/


Tip # 7: Deployment 

Use a python specialized server. Although you can use Apache to deploy a Django project, it doesn’t work so well since it’s not specifically built for python. 

Use Nginx or another CDN. Nginx makes it easier to configure and has a better performance for python projects. 


You can mix web servers and use Nginx + gunicorn and Supervisor to keep it alive. 


Some useful tools :


South: South comes in handy for database migrations. When your models changes it keeps track of the new and old models so you could easily revert in case you need it. It’s customizable so you can use it to go forward or backwards.  Use it to avoid accidents. 


Fabric: Tool for automatic deployments and migrations. Very useful to make deployments among different severs. It effortlessly integrates with python since it is python code. 


Celery:  Task queue manager for asynchronous tasks. Generally speaking, tasks which involve input-output process are slower to perform. For example, creating a PDF invoice, sending complex emails, generating a code and other heavy tasks makes the website slower. If your page is taking some time processing something, chances are that the user might get impatient creating double submits or just abandoning you website. With celery you can perform those tasks that doesn’t require a real-time reply and add it to a queue.


Redis: Redis works as a nosql database. It stores the data inside the cache memory. One of its biggest advantages is that you can use it for sessions. It saves a query to the database making the application faster. You can use it to monitor or any process that requires a counter. It can be used to create statistics, real time calculations and throwing.  


Serenity.  External service. Useful for log creation and cookies. It sends the data to the server and you can later visualize it.  It comes in handy when looking to identify possible bugs. You can make tracebacks, logs, integrate it to Github and even automatically generate tickets (you can also integrate it with other popular helpdesk tools) .


Extra: 

Some Tools for debugging: 


Additional Images




Comments

Add your comment

user-symbol

Stay in touch

Get Practical Tips For Business and Developers.

Learn about PieceX Community Needs for Source Code Projects.

Be the First to Know PieceX Newest Free Community Code Projects.