Simple Log to File example for django 1.3+

I truly love this so much here is your working example! Seriously this is awesome! Start by putting this in your settings.py LOGGING = { ‘version’: 1, ‘disable_existing_loggers’: True, ‘formatters’: { ‘standard’: { ‘format’ : “[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s”, ‘datefmt’ : “%d/%b/%Y %H:%M:%S” }, }, ‘handlers’: { ‘null’: { ‘level’:’DEBUG’, ‘class’:’django.utils.log.NullHandler’, }, ‘logfile’: { ‘level’:’DEBUG’, … Read more

sqlite3.OperationalError: unable to open database file

Django NewbieMistakes PROBLEM You’re using SQLite3, your DATABASE_NAME is set to the database file’s full path, the database file is writeable by Apache, but you still get the above error. SOLUTION Make sure Apache can also write to the parent directory of the database. SQLite needs to be able to write to this directory. Make … Read more

How to set-up a Django project with django-storages and Amazon S3, but with different folders for static files and media files?

I think the following should work, and be simpler than Mandx’s method, although it’s very similar: Create a s3utils.py file: from storages.backends.s3boto import S3BotoStorage StaticRootS3BotoStorage = lambda: S3BotoStorage(location=’static’) MediaRootS3BotoStorage = lambda: S3BotoStorage(location=’media’) Then in your settings.py: DEFAULT_FILE_STORAGE = ‘myproject.s3utils.MediaRootS3BotoStorage’ STATICFILES_STORAGE = ‘myproject.s3utils.StaticRootS3BotoStorage’ A different but related example (that I’ve actually tested) can be seen in … Read more

How to set up a PostgreSQL database in Django

You need to install psycopg2 Python library. Installation Download http://initd.org/psycopg/, then install it under Python PATH After downloading, easily extract the tarball and: $ python setup.py install Or if you wish, install it by either easy_install or pip. (I prefer to use pip over easy_install for no reason.) $ easy_install psycopg2 $ pip install psycopg2 … Read more

ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found: export DJANGO_SETTINGS_MODULE=mysite.settings Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this: heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings –account <your account name> In my specific … Read more

Django – after login, redirect user to his custom page –> mysite.com/username

A simpler approach relies on redirection from the page LOGIN_REDIRECT_URL. The key thing to realize is that the user information is automatically included in the request. Suppose: LOGIN_REDIRECT_URL = ‘/profiles/home’ and you have configured a urlpattern: (r’^profiles/home’, home), Then, all you need to write for the view home() is: from django.http import HttpResponseRedirect from django.urls … Read more

Can I access constants in settings.py from templates in Django?

If it’s a value you’d like to have for every request & template, using a context processor is more appropriate. Here’s how: Make a context_processors.py file in your app directory. Let’s say I want to have the ADMIN_PREFIX_VALUE value in every context: from django.conf import settings # import the settings file def admin_media(request): # return … Read more