Django Tips
These are some of the tips that I need to remember. But there are a lot is not listed here so have fun in digging about them.
Persistent connections
Persistent connections avoid the overhead of re-establishing a connection to the database in each request.
Change CONN_MAX_AGE to enable Persistent connections.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name',
'CONN_MAX_AGE': 600, # 10 minutes
}
}
Securely generate random string
from django.utils.crypto import get_random_string
# Then use it to get the random generated string
get_random_string()
# length and allowed_chars
get_random_string(length=4, allowed_chars='Q2W')
Event Sourcing
https://yoongkang.com/blog/event-sourcing-in-django/
django datetime aware naive
- http://blog.robertorosario.com/timezone-aware-fields-rendering/
- http://www.ilian.io/django-pytz-nonexistenttimeerror-and-ambiguoustimeerror/
- https://julien.danjou.info/blog/2015/python-and-timezones
- http://www.peterbe.com/plog/django-forms-and-making-datetime-inputs-localized
Testing:
- https://lincolnloop.com/blog/introduction-django-selenium-testing/
- http://www.revsys.com/blog/2015/may/29/django-test-plus/
- Mock model_mommy
Debug:
http://dailytechvideo.com/video-192-matt-boehm-python-debuggers/
STATICFILES_STORAGE ManifestStaticFilesStorage
pip install install barrel
"""WSGI application file"""
import os
from django.core.wsgi import get_wsgi_application
from barrel import cooper
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django_app = get_wsgi_application()
auth_decorator = cooper.basicauth(
users=[('someuser', 'somepass'),],
realm='Password Protected'
)
application = auth_decorator(django_app)
Writing Unit Tests for Django Migrations
More resources: