1
1

My OSQA custom module uses a custom tag modelled after OSQA's own question_list_item tag. My tag, like question_list_item, loads a template. This works OK (by adding the import of my tag's .py file to the startup.py of my module), but it breaks when my tag's template tries to load a custom filter library my module uses.

If I move the custom filter and custom tag into the /templatetags folder of the overall OSQA app, everything works fine.

Any idea how to enable templates loaded by my custom tag to be able to load custom tags and filters?

Here's the implementation of my custom tag:

from django import template
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from forum.models import Tag, MarkedTag
from forum.templatetags import argument_parser
from forum import settings

register = template.Library()

class SplunkAppItemNode(template.Node):
    # this template load fails when trying to load my custom filters
    template = template.loader.get_template('modules/splunk/splunk_app_item.html')

def __init__(self, question, options):
        self.question = template.Variable(question)
        self.options = options

def render(self, context):
        return self.template.render(template.Context({
            'question': self.question.resolve(context),
            'favorite_count': self.options.get('favorite_count', 'no') == 'yes',
            'signature_type': self.options.get('signature_type', 'lite'),
        }))

@register.tag
def splunk_app_list_item(parser, token):
    tokens = token.split_contents()[1:]
    return SplunkAppItemNode(tokens[0], argument_parser(tokens[1:]))

Here's a subset of my template to illustrate the failing load:

{% load i18n humanize extra_filters extra_tags user_tags%}
{% load splunk_filters %}
<!-- the line above fails -->
<div class="short-summary">
    <div class="counts">{% if favorite_count %}
    ...

asked 26 Dec '10, 02:57

Justin%20Grant's gravatar image

Justin Grant
1.1k163532
accept rate: 12%

edited 26 Dec '10, 11:03


After experimentation and browsing the django source code for filter and tag loading, I came up with this solution:

  1. create a /templatetags directory underneath your module's top-level directory
  2. add an empty __init.py__ in that folder
  3. move your custom template tag and filter code files into /templatetags
  4. Add a startup.py file to your module's home directory, and put the code below into it:

.

# load this module's custom template tags and filters
from django.template import get_templatetags_modules
from django.utils.importlib import import_module
templatetags_module_name = '%s.templatetags' % __package__
new_module = import_module(templatetags_module_name)
templatetag_modules = get_templatetags_modules()
templatetag_modules.append(new_module.__name__)

What this does is add an additional module (your new templatetags module) into the module list that Django looks through whenever it encounters a <% load %> tag in your templates.

link

answered 27 Dec '10, 00:32

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
×11

Asked: 26 Dec '10, 02:57

Seen: 559 times

Last updated: 27 Dec '10, 00:32

powered by OSQA