addons-server/apps/zadmin/__init__.py

40 строки
1.5 KiB
Python
Исходник Обычный вид История

2009-12-31 01:26:56 +03:00
from django.contrib.admin import options, actions, sites
from django.template import loader
import jingo
def django_to_jinja(template_name, context, **kw):
"""
We monkeypatch Django admin's render_to_response to work in our Jinja
environment. We have an admin/base_site.html template that Django's
templates inherit, but instead of rendering html, it renders the Django
pieces into a Jinja template. We get all of Django's html, but wrapped in
our normal site structure.
"""
context_instance = kw.pop('context_instance')
source = loader.render_to_string(template_name, context, context_instance)
2010-03-01 23:41:48 +03:00
request = context_instance['request']
return jingo.render(request, jingo.env.from_string(source))
2009-12-31 01:26:56 +03:00
actions.render_to_response = django_to_jinja
options.render_to_response = django_to_jinja
sites.render_to_response = django_to_jinja
2010-04-10 07:36:00 +04:00
def jinja_for_django(template_name, context=None, **kw):
"""
If you want to use some built in logic (or a contrib app) but need to
override the templates to work with Jinja, replace the object's
render_to_response function with this one. That will render a Jinja
template through Django's functions. An example can be found in the users
app.
"""
2010-04-10 07:36:00 +04:00
if context is None:
context = {}
context_instance = kw.pop('context_instance')
request = context_instance['request']
for d in context_instance.dicts:
context.update(d)
return jingo.render(request, template_name, context, **kw)