Mar 30

Do not use appname as templatetags module

This is the second time I created same error writing templatetags, so it is about time to document it.

This is the bad layout for poll app.

polls/
    __init__.py
    models.py
    templatetags/
        __init__.py
        polls.py 
    views.py

Instead of polls.py it should be polls_tags.py or something other different from appname.

Whats going on? polls.py would define polls module and it would not be possible to import other modules from original polls application. For example the code bellow would throw ImportError: No module named models

#polls.py
from django import template

from polls.models import Question

register = template.Library()
Mar 07

Announcing FeinCMS Extensions

FeinCMS is great content management system for Django. FeinCMS Extensions is repository of tools and modules that I wrote and find can be of use to other developers.

Visit: http://github.com/bmihelac/feincms-feincmsext

Feb 19

Django time widget with custom time shortcuts

How to customize Django admin time widget to set desired time shortcuts.

This small javascript snippet replaces standard django clock shortcuts Midnight, Noon, Now & 6am with various time increments .

$(window).load(function() {
  $('.timelist').each(function(num, el) {
    time_format = get_format('TIME_INPUT_FORMATS')[0];
    $(el).html('');
    for (i=8; i<20; i++) {
      var time = new Date(1970,1,1,i,0,0);
      lnk = "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + 
                  ", '" + time.strftime(time_format) + "');"
      $(el).append('<li><a href="'lnk'">' + time.strftime('%H:%M') + '</a></li>');
    }
  });
});

This snippet use JQuery which is available with Django 1.2. (dev at time of writing)
Also, javascript should be loaded after DateTimeShortcuts.js but unfortunately I didn’t find a better way to achive this but to add in modified base admin site templates/admin/base_site.html. If you have any suggestion for improvement, let me know in comments.

Jan 27

Django-simpleadmindoc

Simpleadmindoc is django application that allows you to quickly create help for modules in Django admin. Goal is to be flexible enough, fast to create and easy to integrate.

I started this project as I needed to deliver help and documentation for django-admin on client website and admindocs seems more oriented to developers than to users.

http://github.com/bmihelac/django-simpleadmindoc

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',
   ....