Ask Your Question

How to perform Template() on askbot setting?

asked 2011-07-23 08:46:58 -0600

NoahY's avatar

I'm pretty new to Python, and Django is even more unfamiliar, but I was trying to find a simple hack that would process a text-based askbot setting using the Django Template() function, specifically here:

Where what I would like to do something remotely like:

if getattr(askbot_settings, 'FORUM_FAQ',''):
    faq = Template(FORUM_FAQ)
    return render_into_skin(faq, data, request)

which of course doesn't work for various reasons. My question is, what exactly are the intermediate steps to get something like this to work, so that FORUM_FAQ can contain template tags like {{settings.MIN_REP_TO_VOTE_UP}}, be processed to replace these tags, then fed into an HTTP request like faq.htm?

It seems like it should be pretty easy, but again, I don't know either python or django (or askbot) well enough to figure it out.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2011-07-25 10:37:21 -0600

NoahY's avatar


I got it! Sorry for being so dense. Here is the code that works:

def render_text(text, data, request):
    context = RequestContext(request, data)
    skin = get_skin(request)
    template = skin.from_string(text)
    return template.render(context)

This works with both {{settings.SETTING}} and {% trans %} foo {% endtrans %}

Yay :)

edit flag offensive delete link more


pushed to github
NoahY's avatar NoahY  ( 2011-07-25 10:40:47 -0600 )edit

answered 2011-07-24 14:52:06 -0600

NoahY's avatar

Okay, I figured it out... I wasn't importing Template :)

Once I import Template, I add this to

def render_text(text, data, request):
        context = RequestContext(request, data)
        template = Template(text)
        return template.render(context)

And now I can send the text there first, then to render_into_skin.

The only problem I have with this is {% trans %} tags don't work. Otherwise, you can at least include settings variables now.

edit flag offensive delete link more


Hey, sorry for not answering. Askbot uses Jinja2 templates, and they use different template tags for translations. In general, Jinja2 templating engine is better and faster. The only downside is that it is less adopted, I will give you a snippet on how to load a template like that shortly.
Evgeny's avatar Evgeny  ( 2011-07-24 14:54:24 -0600 )edit
great :) I figured it was a jinja issue, but I can't figure out how to access jinja. I've updated the custom faq setting for template tags, and would like to complete it for trans tags as well.
NoahY's avatar NoahY  ( 2011-07-24 14:57:16 -0600 )edit
Very cool, I will take a look at your work tomorrow and probably merge.
Evgeny's avatar Evgeny  ( 2011-07-25 00:18:46 -0600 )edit

answered 2011-07-24 15:04:40 -0600

Evgeny's avatar

updated 2011-07-24 15:07:27 -0600

In askbot, because of the skins module, you need to do this:

from askbot.skins.loaders import get_template
from django.template import Context
template = get_template('some_template.txt')
output = template.render(Context({'some_data': 'some_value'}))

Here are docs for Jinja2. Jinja2 templating engine is used throughout askbot for the speed.

edit flag offensive delete link more


The problem with that is it's a text file... I'm trying to turn a string into a template (an askbot setting). I can do it fine with `template = Template(text)`, it just doesn't like `trans` tags.
NoahY's avatar NoahY  ( 2011-07-24 15:12:16 -0600 )edit
Then you'll need to use "from jinja2 import Template" and then the same code that you used.
Evgeny's avatar Evgeny  ( 2011-07-24 15:14:05 -0600 )edit
File extension does not matter in the templates, but for strings there is Template class within jinja2.
Evgeny's avatar Evgeny  ( 2011-07-24 15:15:44 -0600 )edit
I tried that too :) it still says "unknown tag: trans"
NoahY's avatar NoahY  ( 2011-07-24 15:39:53 -0600 )edit
Even worse, it says "dictionary update sequence element #0 has length 1; 2 is required" even without the trans tags.
NoahY's avatar NoahY  ( 2011-07-24 15:42:57 -0600 )edit

answered 2011-07-25 09:58:39 -0600

NoahY's avatar

updated 2011-07-25 10:20:05 -0600

Okay, this isn't an answer, this is a continuation of the above comments, but I need room to post code :)

Here's my code, at the end of

def render_text(text, data, request):
    env = Environment(extensions=['jinja2.ext.i18n'])
    template = env.from_string(text)
    context = RequestContext(request, data)
    return shortcuts.render_to_string(template, context)

and it returns an error:

dictionary update sequence element #0 has length 1; 2 is required
edit flag offensive delete link more


In cases like this you can insert lines "import pdb" and "pdb.set_trace()" just before the failure point and try different solutions right in the python shell prompt. Looks like a plain python runtime error. You can hit "s" in the debugger and see exactly where the code breaks. Also - for debugging Jinja2 set DEBUG = True, TEMPLATE_DEBUG = False in the - it is weird but this is the way it works.
Evgeny's avatar Evgeny  ( 2011-07-25 10:23:53 -0600 )edit
Yeah, the latter is what I've been doing... I've never used the python shell, but it sounds helpful :)
NoahY's avatar NoahY  ( 2011-07-25 10:41:41 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-07-23 08:46:58 -0600

Seen: 1,110 times

Last updated: Jul 25 '11