Posts tagged Django
A neat little render_to_response trick
Aug 17th
If you’ve used Django long enough, you will have noticed that render_to_response uses Context rather than RequestContext as the context instance. This is just no fun if you want to use things like auth, MEDIA_URL, etc in your templates. You could just add context_instance=RequestContext(request) to every render_to_response call, or you can use this little code snippet. Not only does it set the default context instance to RequestContext, but it’s a little less typing. In my projects, I put this in Commons.utils.
from django.template import RequestContext from django.shortcuts import render_to_response def render(request, *kargs, **kwargs): kwargs["context_instance"] = RequestContext(request) return render_to_response(*kargs, **kwargs)
Use it just as you would render_to_response, with the exception of setting context_instance. Enjoy!
The “Better” settings.py
Aug 17th
While working on my CRM, I had a bit of an issue: I wanted to keep my setup specific settings (Database information, timezone, secret key, etcs) separate from application settings (INSTALLED_APPS, ROOT_URLCONF, etc). This is useful since it keeps development configurations and production configurations separate and it keeps your setup specific settings out of your VCS. Well, less talk and more code!
# local_settings.py
ADMINS = (
('Your Default User', 'nobody@example.com'),
)
MANAGERS = ADMINS
PRODUCTION = False
TIME_ZONE = 'America/Denver'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = False
SECRET_KEY = 'PutASecretKeyHere'
DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
PROJECT_ROOT = '/path/to/your/project'
MEDIA_URL = 'http://your/media/path/'
# Settings.py
from local_settings import *
if not PRODUCTION:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
else:
DEBUG = False
TEMPLATE_DEBUG = False
MEDIA_ROOT = '%s/templates/media' % (PROJECT_ROOT)
ADMIN_MEDIA_PREFIX = '/media/'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media')
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
TEMPLATE_DIRS = (
'%s/templates' % (PROJECT_ROOT)
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
)
from post_settings import *
# post_settings.py
TEMPLATE_DIRS.append("/an/additional/template/path/")
So, how does it work? Quite simply. Put your database settings into local_settings.py and any other settings that aren’t set in settings.py will go there as well. Need to override something in settings.py? Use post_settings.py for that. Not cool enough? In local_settings.py, use a conditional to setup your database settings! This will look something like this:
if PRODUCTION:
DATABASE_TYPE = 'mysql'
[...]
else:
DATABASE_TYPE = 'sqlite3'
[...]
Pretty nifty, eh? I figured it might make someone’s life a little easier.
Cheers!
Nick