Nov 12

Django set language for admin

Middleware that intialize specific locale for admin pages.

If you want to explicitly set language for django admin section use this middleware:

from django.conf import settings

class AdminLocaleURLMiddleware:

    def process_request(self, request):
        if request.path.startswith('/admin'):
            request.LANG = getattr(settings, 'ADMIN_LANGUAGE_CODE', settings.LANGUAGE_CODE)
            translation.activate(request.LANG)
            request.LANGUAGE_CODE = request.LANG

Then put somewhere in settings.py:

ADMIN_LANGUAGE_CODE=’it’

and add middleware:

MIDDLEWARE_CLASSES = (
   ...
    'utils.multilang.middleware.AdminLocaleURLMiddleware',
   ....
Nov 10

Can't connect to MySQL when touching django.wsgi

Error when touching django.wsgi: Can’t connect to MySQL server…

did not help unless restarted apache.

Solution that did work is to set DATABASE_PORT to ‘3306’ instead of empty string. Not sure why…

Oct 23

Django - avoiding typing password for superuser

When developing Django applications I call some kind of flush.py script innumerable times to recreate and sync database, import some data and create super user. It looks like:


#!/usr/bin/env bash
mysql -u root -e "DROP DATABASE secret_project_db;" 
mysql -u root -e "CREATE DATABASE secret_project_db CHARACTER SET='utf8';" 
./manage.py syncdb --noinput
./manage.py loaddata app/fixtures/*
./manage.py createsuperuser --username=admin --email=admin@example.com

It does the job quick but the thing that really annoy me is entering password and password confirmation each time over and over again. In looking for a quick way to reduce typing this is what I have now instead of last line in previous example:


echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', admin@example.com', 'pass')" | ./manage.py shell

Hope it will save you some typing too, if anyone has better idea then to call shell let me know in the comments.

Sep 15

Use unicode in doctests

Use hex representation, double quote slashes in expectation. Do not use print in doctests .

""" 
>>> s = u"šđčćž" 
>>> s
u'\\xc5\\xa1\\xc4\\x91\\xc4\\x8d\\xc4\\x87\\xc5\\xbe'
""" 
May 11

Django testshell

This commands runs a Python interactive interpreter with test database and data from the given fixture(s).

It is usable if you want to play with test database.

See also testserver docs

from django.core.management.base import BaseCommand

from optparse import make_option

class Command(BaseCommand):
    option_list = BaseCommand.option_list
    help = 'Runs a Python interactive interpreter with test database and data from the given fixture(s).'
    args = '[fixture ...]'

    requires_model_validation = False

    def handle(self, *fixture_labels, **options):
        from django.core.management import call_command
        from django.db import connection

        verbosity = int(options.get('verbosity', 1))

        # Create a test database.
        db_name = connection.creation.create_test_db(verbosity=verbosity)

        # Import the fixture data into the test database.
        call_command('loaddata', *fixture_labels, **{'verbosity': verbosity})

        call_command('shell')