Commit f6bf45c 1 parent b669d7f commit f6bf45c Copy full SHA for f6bf45c
File tree 5 files changed +46
-5
lines changed
5 files changed +46
-5
lines changed Original file line number Diff line number Diff line change 5
5
Unlike other form rendering packages, below template tags
6
6
7
7
``` bash
8
+ {% form_tag %}
8
9
{% render_form %}
9
10
{% render_submit %}
10
11
{% render_field %}
@@ -34,6 +35,7 @@ FORMIFY = {
34
35
Leveraging Python OOP, you can override some methods of the formify helper to customize the rendering behavior.
35
36
36
37
``` bash
38
+ {% form_tag %} -> formify_helper.render_form_tag
37
39
{% render_form %} -> formify_helper.render_form
38
40
{% render_submit %} -> formify_helper.render_submit
39
41
{% render_field %} -> formify_helper.render_field
Original file line number Diff line number Diff line change 1
1
# Template Tags
2
2
3
+ A typical case of using formify in a template is like this:
4
+
5
+ ``` html
6
+ {% load formify %}
7
+
8
+ {% form_tag form action=url %}
9
+
10
+ {% csrf_token %}
11
+
12
+ {% render_form form %}
13
+
14
+ {% render_submit text='Submit' css_class="btn btn-primary" %}
15
+
16
+ {% endform_tag %}
17
+ ```
18
+
19
+ ## form_tag
20
+
21
+ 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.
22
+
3
23
## render_form
4
24
5
- This tag can render form or formset.
25
+ This tag can render ` form ` or ` formset ` .
6
26
7
27
It will iterate and render all form fields automatically.
8
28
Original file line number Diff line number Diff line change 1
1
{% if self.should_render %}
2
2
< label for ="{{ self.field.id_for_label }} " class ="{{ self.formify_helper.label_class }} ">
3
- {{ self.field.label|safe }} {{ self.asterisk_if_required|safe }}
3
+ {# make sure no space between label and asterisk #}
4
+ {{ self.field.label|safe }}{{ self.asterisk_if_required|safe }}
4
5
</ label >
5
6
{% endif %}
Original file line number Diff line number Diff line change @@ -91,7 +91,7 @@ def render(self, context: Context):
91
91
key : safe_resolve (kwarg , context )
92
92
for key , kwarg in self .context_kwargs .items ()
93
93
}
94
- form = resolved_component_args [0 ]
94
+ form = resolved_component_args [0 ] if resolved_component_args else None
95
95
formify_helper = init_formify_helper_for_form (form )
96
96
content = self .nodelist .render (context )
97
97
return formify_helper .render_form_tag (
@@ -121,9 +121,9 @@ def do_form_tag(parser, token):
121
121
f"Internal error: Expected tag_name to be { tag_name } , but it was { tag_args [0 ].token } "
122
122
)
123
123
124
- if len (tag_args ) != 2 :
124
+ if len (tag_args ) > 2 :
125
125
raise TemplateSyntaxError (
126
- f"'{ tag_name } ' tag should have form as the first argument, other arguments should be keyword arguments."
126
+ f"'{ tag_name } ' tag only accepts form as the first argument, other arguments should be keyword arguments."
127
127
)
128
128
129
129
context_args = tag_args [1 :]
Original file line number Diff line number Diff line change @@ -28,6 +28,24 @@ def test_form_tag(self):
28
28
assert_select (html , "form[method='POST']" , 1 )
29
29
assert_select (html , "form[action='/']" , 1 )
30
30
31
+ # should still work if do not pass in form
32
+ template = Template (
33
+ """
34
+ {% load formify %}
35
+ {% with url='/' %}
36
+ {% form_tag action=url %}
37
+ {% render_form form %}
38
+ {% endform_tag %}
39
+ {% endwith %}
40
+ """
41
+ )
42
+ c = Context ({"form" : SampleForm ()})
43
+ html = template .render (c )
44
+
45
+ assert_select (html , "form" , 1 )
46
+ assert_select (html , "form[method='POST']" , 1 )
47
+ assert_select (html , "form[action='/']" , 1 )
48
+
31
49
def test_form_tag_extra_kwargs (self ):
32
50
template = Template (
33
51
"""
You can’t perform that action at this time.
0 commit comments