Django.guru

Add Hash to Static Files Names in Django

August 16, 2022
Configuration
static

It is very useful to append hash at the end of static files name such as js/css to prevent situation when website visitors after update still have old version of the file in browser cache. Django have built-in tool which automatically add hash to the all static files of Django project on python manage.py collectstatic You need just to add this line to the settings.py file STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' That’s all. ...

Django Colored Output in PyCharm Run Configurations

August 16, 2022
Development Environment
PyCharm

Starting using PyCharm IDE for Django development you migt face with issue that default output of the PyCharm “Run Configuration” or “Services” panes colored by red regardless is it error or info line. You need to add this file colored_logger.py to the app directory (same directory where settings.py stored) and import it inside the settings.py file import app.colored_logger Your output will become

Use Environment Variables in Django - Efficient Way

August 16, 2022
Infrastructure
env

If you stil not use environment variables to configure your Django application - dont wait, do it! I’ll tell you efficient way to use environment variables in the Django’s settings.py file which allows you to set environment variables via the server environment and in the .env file for local development. Installation # You need to install python-decouple package from PyPi. pip pip install python-decouple poetry poetry add python-decouple pipenv pipenv install python-decouple Usage in settings. ...

How to Use Multiple Databases in Django

August 15, 2022
Database
database, orm

Django ORM very well designed to use multiple relational databases (regardless engine) in single project. To start using two databases in Django project you need to configure connection to all of them in the settings.py file.

...

Set database tables prefix in Django

August 14, 2022
Database
database, packages

Sometimes when you using same database by different applications it is a good (or required) to separate tables between apps to avoid any conflicts. In this case you can find useful django-database-prefix library available on PyPi Installation # pip pip install django-database-prefix poetry poetry add django-database-prefix pipenv pipenv install django-database-prefix After package installation you need to add it to the your project’s settings.py file on first place. 'django_database_prefix', 'django.contrib.admin', 'django.contrib.auth', 'django. ...