1
1

My OSQA site's header requires custom presentation logic beyond what I can do using client-side custom header HTML available via the admin site.

Currently I've edited OSQA's own header.html and my site works fine, but I'd prefer to keep my code encapsulated in a single OSQA module.

Is there a way, inside a module, to force OSQA to use my version of a template as opposed to OSQA's version?

asked 25 Dec '10, 02:34

Justin%20Grant's gravatar image

Justin Grant
1.1k163532
accept rate: 12%

edited 31 Dec '10, 11:16

rickross's gravatar image

rickross ♦♦
12.5k2914972


I worked out a solution which seems to work OK. My approach isn't appropriate when creating a whole new skin, but if you simply want to override a few templates it will probably work fine.

First, I created a template_overrides directory underneath my module's root directory. Any template files put there will be used instead of files with the same name inside the OSQA skins folders.

Second, I added the code below to __init.py__ of my OSQA module, which will load templates from that new folder:

import os
from django.template import TemplateDoesNotExist

def template_overrides_loader(name, dirs=None):
    file_name = os.path.join(os.path.dirname(__file__),'template_overrides', name)

if os.path.exists(file_name):
        try:
            f = open(file_name, 'r')
            source = f.read()
            f.close()
            return (source, file_name)
        except:
            pass

raise TemplateDoesNotExist, name

template_overrides_loader.is_usable = True

Third, in settings_local.py for my OSQA site, I added the custom loader above as the first entry in the list of template loaders that Django uses. This overrides the folders set in settings.py.

# insert our override loader first
# TODO: optimize this, e.g. using the Django caching template loader
TEMPLATE_LOADERS = (
    'forum_modules.splunk.template_overrides_loader',
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
    'forum.modules.module_templates_loader',
    'forum.skins.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)
link

answered 27 Dec '10, 13:58

Justin%20Grant's gravatar image

Justin Grant
1.1k163532
accept rate: 12%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×24
×23

Asked: 25 Dec '10, 02:34

Seen: 722 times

Last updated: 26 Aug '11, 04:03

powered by OSQA