Monday, April 18, 2011

Django Themes

Hello there.
Today I want to tell about Django application I developed couple month ago. I was working on Escalibro project (as team work on classes) and can't really decide how design should be.
So occasionally we decide that our service should have themes, so user can choose between couple of them, and use whatever he wants.
After googling around "django themes" I found only some strange app with "django-themes" name, and no actual source code on it.And I decided to write my own one.
It actually was pretty easy. Django has STATIC_URL and MEDIA_URL for pointing where images, css, js and stuff is - so I just needed Middleware that will change this variables on flight. (By the way, we use MEDIA_URL = STATIC_URL, so I change only STATIC_URL, but it can be easily fixed). But that wasn't all I wanted - I wanted that all design (not just images and css) can be changed. So I write TemplateLoader that search for templates, depends what theme is now up. And this is got my possibility to override default theme's template on another theme - so full freedom to make whatever designer wants.
Plus, after beta testing, we needed to make release theme - theme where css files are joined to one file, js files are obfuscated, etc. So I added DEBUG_THEME parameter and use it as indicator is now debug or release mode.
In result I uploaded all that code as application on github:
https://github.com/ilblackdragon/django-themes (fork it! =)

So short "how to use":

  1. Install it thought 
    pip install git+git://github.com/ilblackdragon/django-themes
  2. Add to settings.py: 
    INSTALLED_APPS += ('themes', )
  3. And next you need to make themes. Add config file themes_settings.py with next content:
    import os.path
    from themes.core import Theme, ThemesManager
    from django.utils.translation import ugettext_lazy as _
    
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    
    THEMES_MANAGER = ThemesManager()
    
    THEMES_MANAGER.add_theme(Theme(
            name = _("Default"),
            description = _("Default theme"),
            screenshot = "/static/default/screenshot.png",
            template_dir = os.path.join(PROJECT_ROOT, "templates/default"),
            media_url = "/static/default/",
    ))
    
    THEMES_MANAGER.set_default(0)
    
  4. Add line to your urls:
      (r'^themes/', include('themes.urls')),
    
  5. Sync data base and you ready to go
    ./manage.py syncdb
So what do this code doing? First you create Themes manager - it's just container of all themes you have.
Next you add theme - name, description, screenshot - this all will be shown on page, where user can choose whatever theme he want to use.
And next - template_dir - it's dir where all you templates are, and media_url - where your images and stuff are.
So pretty easy, instead of putting all templates in /templates/ folder - you just put them on /template/default. But when you want add new theme, you create folder, and create template, that will override template from default theme.
In few words - just try it =)

PS. We actually failed in have couple themes, but may be soon we will have new themes on Escalibro. Join us there ;)

No comments:

Post a Comment