Skip to content

Commit

Permalink
Taking care of deprecation warning: 'render() must be called with a d…
Browse files Browse the repository at this point in the history
…ict, not a Context.'
  • Loading branch information
gregmuellegger committed Apr 20, 2015
1 parent e8bc9b6 commit 06b9d5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
26 changes: 26 additions & 0 deletions floppyforms/compat.py
@@ -0,0 +1,26 @@
import django
from django.template import Context


if django.VERSION < (1, 8):
def get_template(context, template_name):
from django.template.loader import get_template
return get_template(template_name)

def get_context(context):
# Django < 1.8 only wants ``Context`` instances as context, no dict
# instances.
if not isinstance(context, Context):
return Context(context)
return context

else:
def get_template(context, template_name):
# Django 1.8 and higher support multiple template engines. We need to
# load child templates used in the floppyform template tags from the
# same engine. Otherwise this might get really confusing.
return context.template.engine.get_template(template_name)

def get_context(context):
# Django 1.8 only wants dicts as context, no ``Context`` instances.
return context
19 changes: 7 additions & 12 deletions floppyforms/forms.py
@@ -1,29 +1,24 @@
from django import forms, template
from django import forms
from django.template.loader import get_template
from django.utils.encoding import python_2_unicode_compatible

from .templatetags.floppyforms import FormNode
from .compat import get_context


__all__ = ('BaseForm', 'Form',)


@python_2_unicode_compatible
class LayoutRenderer(object):
_template_node = FormNode(
'form',
[template.Variable('form')],
{
'using': template.Variable('layout'),
'only': False,
'with': None,
})
_render_as_template_name = 'floppyforms/_render_as.html'

def _render_as(self, layout):
context = template.Context({
template_node = get_template(self._render_as_template_name)
context = get_context({
'form': self,
'layout': layout,
})
return self._template_node.render(context)
return template_node.render(context)

def __str__(self):
return self._render_as('floppyforms/layouts/default.html')
Expand Down
3 changes: 3 additions & 0 deletions floppyforms/templates/floppyforms/_render_as.html
@@ -0,0 +1,3 @@
{# A helper template used in the Form.as_* methods. #}
{% load floppyforms %}
{% form form using layout %}
8 changes: 5 additions & 3 deletions floppyforms/templatetags/floppyforms.py
Expand Up @@ -10,9 +10,11 @@
from django.template import (Library, Node, Variable,
TemplateSyntaxError, VariableDoesNotExist)
from django.template.base import token_kwargs
from django.template.loader import get_template
from django.utils.functional import empty

from ..compat import get_template


register = Library()


Expand Down Expand Up @@ -448,7 +450,7 @@ def get_nodelist(self, context, extra_context):
template_name = self.options['using'].resolve(context)
else:
template_name = self.get_template_name(context)
return get_template(template_name)
return get_template(context, template_name)
except:
if settings.TEMPLATE_DEBUG:
raise
Expand Down Expand Up @@ -693,7 +695,7 @@ def render(self, context):
widget_ctx = {'field': field}
template = 'floppyforms/dummy.html'

template = get_template(template)
template = get_template(context, template)
context.update(widget_ctx)
rendered = template.render(context)
context.pop()
Expand Down

0 comments on commit 06b9d5c

Please sign in to comment.