Skip to content

Commit

Permalink
Feature/form tag (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin authored Sep 15, 2024
1 parent b669d7f commit f6bf45c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/source/formify_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Unlike other form rendering packages, below template tags

```bash
{% form_tag %}
{% render_form %}
{% render_submit %}
{% render_field %}
Expand Down Expand Up @@ -34,6 +35,7 @@ FORMIFY = {
Leveraging Python OOP, you can override some methods of the formify helper to customize the rendering behavior.

```bash
{% form_tag %} -> formify_helper.render_form_tag
{% render_form %} -> formify_helper.render_form
{% render_submit %} -> formify_helper.render_submit
{% render_field %} -> formify_helper.render_field
Expand Down
22 changes: 21 additions & 1 deletion docs/source/tags.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# Template Tags

A typical case of using formify in a template is like this:

```html
{% load formify %}

{% form_tag form action=url %}

{% csrf_token %}

{% render_form form %}

{% render_submit text='Submit' css_class="btn btn-primary" %}

{% endform_tag %}
```

## form_tag

This tag is to render the form tag, it can help add some attributes to the form tag from the parameters from the template tag.

## render_form

This tag can render form or formset.
This tag can render `form` or `formset`.

It will iterate and render all form fields automatically.

Expand Down
3 changes: 2 additions & 1 deletion src/django_formify/components/formify/tw/field_label.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% if self.should_render %}
<label for="{{ self.field.id_for_label }}" class="{{ self.formify_helper.label_class }}">
{{ self.field.label|safe }} {{ self.asterisk_if_required|safe }}
{# make sure no space between label and asterisk #}
{{ self.field.label|safe }}{{ self.asterisk_if_required|safe }}
</label>
{% endif %}
6 changes: 3 additions & 3 deletions src/django_formify/templatetags/formify.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def render(self, context: Context):
key: safe_resolve(kwarg, context)
for key, kwarg in self.context_kwargs.items()
}
form = resolved_component_args[0]
form = resolved_component_args[0] if resolved_component_args else None
formify_helper = init_formify_helper_for_form(form)
content = self.nodelist.render(context)
return formify_helper.render_form_tag(
Expand Down Expand Up @@ -121,9 +121,9 @@ def do_form_tag(parser, token):
f"Internal error: Expected tag_name to be {tag_name}, but it was {tag_args[0].token}"
)

if len(tag_args) != 2:
if len(tag_args) > 2:
raise TemplateSyntaxError(
f"'{tag_name}' tag should have form as the first argument, other arguments should be keyword arguments."
f"'{tag_name}' tag only accepts form as the first argument, other arguments should be keyword arguments."
)

context_args = tag_args[1:]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ def test_form_tag(self):
assert_select(html, "form[method='POST']", 1)
assert_select(html, "form[action='/']", 1)

# should still work if do not pass in form
template = Template(
"""
{% load formify %}
{% with url='/' %}
{% form_tag action=url %}
{% render_form form %}
{% endform_tag %}
{% endwith %}
"""
)
c = Context({"form": SampleForm()})
html = template.render(c)

assert_select(html, "form", 1)
assert_select(html, "form[method='POST']", 1)
assert_select(html, "form[action='/']", 1)

def test_form_tag_extra_kwargs(self):
template = Template(
"""
Expand Down

0 comments on commit f6bf45c

Please sign in to comment.