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:
http://askbot.org/en/question/519/faq-to-settings
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.
Ooooooooo!
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 :)
Okay, I figured it out... I wasn't importing Template :)
Once I import Template, I add this to loaders.py:
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.
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.
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 loaders.py:
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
Create your Q&A site at askbot.com. Managed Askbot hosting at just $15/mo. Dedicated hosting, support contracts, consulting services.
create your Q&A siteAsked: 2011-07-23 08:46:58 -0500
Seen: 125 times
Last updated: Jul 25 '11
can I run askbot on tornado instead of django
sharing templates between askbot and our own django app
openid error occured during login/signup during signup process of askbot in m own application
How to internationalize counts say for Russian in Django?
How to configure Apache to run Django application?
How to customize messages in django applications?
Does askbot work with django version 1.2.3?
Copyright Askbot, 2010-2011. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.