Skip to content

Commit

Permalink
BaseInput subclasses value can now be set to a context variable
Browse files Browse the repository at this point in the history
  • Loading branch information
maraujop committed Sep 16, 2012
1 parent a4ea988 commit d9785ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion crispy_forms/layout.py
Expand Up @@ -214,8 +214,10 @@ def __init__(self, name, value, **kwargs):

def render(self, form, form_style, context):
"""
Renders an `<input />` if container is used as a Layout object
Renders an `<input />` if container is used as a Layout object.
Input button value can be a variable in context.
"""
self.value = Template(unicode(self.value)).render(context)
return render_to_string(self.template, Context({'input': self}))


Expand Down
9 changes: 6 additions & 3 deletions crispy_forms/tests/tests.py
Expand Up @@ -623,7 +623,8 @@ def test_second_layout_multifield_column_buttonholder_submit_div(self):
css_class = "columns",
),
ButtonHolder(
Submit('Save the world', 'Save', css_class='button white', data_id='test', data_name='test'),
Submit('Save the world', '{{ value_var }}', css_class='button white', data_id='test', data_name='test'),
Submit('store', 'Store results')
),
Div(
'password1',
Expand All @@ -639,7 +640,7 @@ def test_second_layout_multifield_column_buttonholder_submit_div(self):
{% load crispy_forms_tags %}
{% crispy form form_helper %}
""")
c = Context({'form': TestForm(), 'form_helper': form_helper})
c = Context({'form': TestForm(), 'form_helper': form_helper, 'value_var': "Save"})
html = template.render(c)

self.assertTrue('multiField' in html)
Expand All @@ -655,11 +656,13 @@ def test_second_layout_multifield_column_buttonholder_submit_div(self):
self.assertTrue('data-id="test"' in html)
self.assertTrue('data-name="test"' in html)
self.assertTrue('name="save-the-world"' in html)
self.assertTrue('value="Save"' in html)
self.assertTrue('name="store"' in html)
self.assertTrue('value="Store results"' in html)
self.assertTrue('id="custom-div"' in html)
self.assertTrue('class="customdivs"' in html)
self.assertTrue('test-markup="123"' in html)


def test_layout_within_layout(self):
form_helper = FormHelper()
form_helper.add_layout(
Expand Down

0 comments on commit d9785ec

Please sign in to comment.