@@ -104,14 +104,15 @@ def prepare_css_container(self):
104
104
105
105
def get_context_data (self , context_data ) -> Context :
106
106
if isinstance (context_data , Context ):
107
- new_context = Context ( context_data . flatten ())
107
+ context = context_data
108
108
else :
109
- new_context = Context (context_data )
109
+ context = Context (context_data )
110
110
111
- new_context ["formify_helper" ] = self
112
- new_context ["form" ] = self .form
113
- new_context ["formset" ] = self .formset
114
- return new_context
111
+ context ["formify_helper" ] = self
112
+ context ["form" ] = self .form
113
+ context ["formset" ] = self .formset
114
+
115
+ return context
115
116
116
117
def smart_render (self , template , context ):
117
118
# if template is django.template.base.Template, make sure context is a Context object
@@ -126,6 +127,7 @@ def smart_render(self, template, context):
126
127
else :
127
128
# make sure the context is dict
128
129
if isinstance (context , Context ):
130
+ # convert to dict
129
131
context_for_render = context .flatten ()
130
132
else :
131
133
context_for_render = context
@@ -139,97 +141,100 @@ def build_default_layout(self):
139
141
# Rendering Methods
140
142
################################################################################
141
143
142
- def render_formset (self , context , create_new_context = False ):
144
+ def render_form_tag (self , context , content , ** kwargs ):
145
+ with context .push ():
146
+ update_context = self .get_context_data (context )
147
+ update_context ["form_content" ] = content
148
+ attrs = {
149
+ "class" : kwargs .pop ("css_class" , "" ),
150
+ "method" : kwargs .pop ("method" , "POST" ).upper (),
151
+ }
152
+ action = kwargs .pop ("action" , "" )
153
+ if action :
154
+ attrs ["action" ] = action
155
+ # add extra attributes
156
+ for key , value in kwargs .items ():
157
+ attrs [key ] = value
158
+ update_context ["attrs" ] = attrs
159
+ template = get_template ("formify/tailwind/form_tag.html" )
160
+ return self .smart_render (template , update_context )
161
+
162
+ def render_formset (self , context ):
143
163
"""
144
164
uni_formset.html
145
165
"""
146
- if create_new_context :
147
- context = self .get_context_data (context )
148
-
149
166
# render formset management form fields
150
167
management_form = self .formset .management_form
151
168
management_form_helper = init_formify_helper_for_form (management_form )
152
- management_form_html = management_form_helper . render_form (
153
- management_form_helper .get_context_data (context )
154
- )
169
+ with context . push ():
170
+ update_context = management_form_helper .get_context_data (context )
171
+ management_form_html = management_form_helper . render_form ( update_context )
155
172
156
173
# render formset errors
157
174
formset_errors = self .render_formset_errors (context )
158
175
159
176
forms_html = ""
160
177
for form in self .formset :
161
178
form_helper = init_formify_helper_for_form (form )
162
- forms_html += form_helper .render_form (form_helper .get_context_data (context ))
179
+ with context .push ():
180
+ update_context = form_helper .get_context_data (context )
181
+ forms_html += form_helper .render_form (update_context )
163
182
164
183
return SafeString (management_form_html + formset_errors + forms_html )
165
184
166
- def render_form (self , context , create_new_context = False ):
185
+ def render_form (self , context ):
167
186
"""
168
187
uni_form.html
169
188
"""
170
- if create_new_context :
171
- context = self .get_context_data (context )
172
-
173
189
return SafeString (
174
190
self .render_form_errors (context ) + self .render_form_fields (context )
175
191
)
176
192
177
- def render_field (self , field , context , create_new_context = False , ** kwargs ):
193
+ def render_field (self , context , field , ** kwargs ):
178
194
"""
179
195
This method is to render specific field
180
196
"""
181
- helper : FormifyHelper = self
182
-
183
- if create_new_context :
184
- # create a new instance of FormifyHelper
185
- field_helper = copy .copy (self )
197
+ field_formify_helper = copy .copy (self )
186
198
187
- # assign extra kwargs to field_helper
188
- for key , value in kwargs .items ():
189
- setattr (field_helper , key , value )
199
+ # assign extra kwargs to formify_helper if needed
200
+ for key , value in kwargs .items ():
201
+ setattr (field_formify_helper , key , value )
190
202
191
- context = field_helper .get_context_data (context )
192
-
193
- helper = field_helper
194
- else :
195
- pass
203
+ with context .push ():
204
+ context ["field" ] = field
196
205
197
- context ["field" ] = field
198
-
199
- if field .is_hidden :
200
- return SafeString (field .as_widget ())
201
- else :
202
- dispatch_method_callable = helper .field_dispatch (field )
203
- return SafeString (dispatch_method_callable (context ))
206
+ if field .is_hidden :
207
+ return SafeString (field .as_widget ())
208
+ else :
209
+ dispatch_method_callable = field_formify_helper .field_dispatch (field )
210
+ update_context = field_formify_helper .get_context_data (context )
211
+ return SafeString (dispatch_method_callable (update_context ))
204
212
205
- def render_submit (self , context , create_new_context = True , ** kwargs ):
213
+ def render_submit (self , context , ** kwargs ):
206
214
"""
207
215
It would be called from the render_submit tag
208
216
209
217
Here we use Submit component to render the submit button, you can also override this method and
210
218
use Django's get_template and render methods to render the submit button
211
219
"""
212
- if create_new_context :
213
- context = self .get_context_data (context )
214
-
215
220
css_class = kwargs .pop ("css_class" , None )
216
221
text = kwargs .pop ("text" , None )
217
222
submit_component = Submit (text = text , css_class = css_class , ** kwargs )
218
- return submit_component . render_from_parent_context ( context )
219
-
220
- def render_formset_errors ( self , context , create_new_context = False ):
221
- if create_new_context :
222
- context = self . get_context_data ( context )
223
-
224
- error_template = get_template ( "formify/tailwind/errors_formset.html" )
225
- return self .smart_render ( error_template , context )
226
-
227
- def render_form_errors ( self , context , create_new_context = False ):
228
- if create_new_context :
229
- context = self . get_context_data ( context )
230
-
231
- error_template = get_template ( "formify/tailwind/errors.html" )
232
- return self .smart_render (error_template , context )
223
+ with context . push ():
224
+ update_context = self . get_context_data ( context )
225
+ return submit_component . render_from_parent_context ( update_context )
226
+
227
+ def render_formset_errors ( self , context ):
228
+ template = get_template ( "formify/tailwind/errors_formset.html" )
229
+ with context . push ():
230
+ update_context = self .get_context_data ( context )
231
+ return self . smart_render ( template , update_context )
232
+
233
+ def render_form_errors ( self , context ) :
234
+ template = get_template ( "formify/tailwind/errors.html" )
235
+ with context . push ():
236
+ update_context = self . get_context_data ( context )
237
+ return self .smart_render (template , update_context )
233
238
234
239
################################################################################
235
240
@@ -251,9 +256,10 @@ def field_dispatch(self, field):
251
256
def render_form_fields (self , context ):
252
257
if not self .layout :
253
258
self .layout = self .build_default_layout ()
254
-
255
- # render_from_parent_context is a method from the viewcomponent class
256
- return self .layout .render_from_parent_context (context )
259
+ with context .push ():
260
+ update_context = self .get_context_data (context )
261
+ # render_from_parent_context is a method from the Component class
262
+ return self .layout .render_from_parent_context (update_context )
257
263
258
264
def render_as_tailwind_field (self , context ):
259
265
"""
0 commit comments