def render_layout(self, form, context, template_pack=TEMPLATE_PACK): """ Returns safe html of the rendering of the layout """ form.rendered_fields = set() # This renders the specifed Layout html = self.layout.render(form, self.form_style, context, template_pack=template_pack) if self.render_unmentioned_fields: fields = set(form.fields.keys()) left_fields_to_render = fields - form.rendered_fields for field in left_fields_to_render: html += render_field(field, form, self.form_style, context, template_pack=template_pack) # If the user has Meta.fields defined, not included in the layout # we suppose they need to be rendered. Otherwise we render the # layout fields strictly if hasattr(form, 'Meta'): if hasattr(form.Meta, 'fields'): current_fields = set(getattr(form, 'fields', [])) meta_fields = set(getattr(form.Meta, 'fields')) fields_to_render = current_fields & meta_fields left_fields_to_render = fields_to_render - form.rendered_fields for field in left_fields_to_render: html += render_field(field, form, self.form_style, context) return mark_safe(html)
def render_layout(self, form, context, template_pack=TEMPLATE_PACK): """ Returns safe html of the rendering of the layout """ form.rendered_fields = set() form.crispy_field_template = self.field_template # This renders the specified Layout strictly html = self.layout.render( form, self.form_style, context, template_pack=template_pack ) # Rendering some extra fields if specified if self.render_unmentioned_fields or self.render_hidden_fields or self.render_required_fields: fields = set(form.fields.keys()) left_fields_to_render = fields - form.rendered_fields for field in left_fields_to_render: if ( self.render_unmentioned_fields or self.render_hidden_fields and form.fields[field].widget.is_hidden or self.render_required_fields and form.fields[field].widget.is_required ): html += render_field( field, form, self.form_style, context, template_pack=template_pack ) # If the user has Meta.fields defined, not included in the layout, # we suppose they need to be rendered if hasattr(form, 'Meta'): if hasattr(form.Meta, 'fields'): current_fields = tuple(getattr(form, 'fields', {}).keys()) meta_fields = getattr(form.Meta, 'fields') fields_to_render = list_intersection(current_fields, meta_fields) left_fields_to_render = list_difference(fields_to_render, form.rendered_fields) for field in left_fields_to_render: # We still respect the configuration of the helper # regarding which fields to render if ( self.render_unmentioned_fields or (self.render_hidden_fields and form.fields[field].widget.is_hidden) or (self.render_required_fields and form.fields[field].widget.is_required) ): html += render_field(field, form, self.form_style, context) return mark_safe(html)
def render_layout(self, form, context, template_pack=TEMPLATE_PACK): """ Returns safe html of the rendering of the layout """ form.rendered_fields = set() form.crispy_field_template = self.field_template # This renders the specifed Layout strictly html = self.layout.render( form, self.form_style, context, template_pack=template_pack ) # Rendering some extra fields if specified if self.render_unmentioned_fields or self.render_hidden_fields or self.render_required_fields: fields = set(form.fields.keys()) left_fields_to_render = fields - form.rendered_fields for field in left_fields_to_render: if ( self.render_unmentioned_fields or self.render_hidden_fields and form.fields[field].widget.is_hidden or self.render_required_fields and form.fields[field].widget.is_required ): html += render_field( field, form, self.form_style, context, template_pack=template_pack ) # If the user has Meta.fields defined, not included in the layout, # we suppose they need to be rendered if hasattr(form, 'Meta'): if hasattr(form.Meta, 'fields'): current_fields = set(getattr(form, 'fields', [])) meta_fields = set(getattr(form.Meta, 'fields')) # FIXME - remove this line? #fields_to_render = current_fields & meta_fields left_fields_to_render = fields_to_render - form.rendered_fields for field in left_fields_to_render: html += render_field(field, form, self.form_style, context) return mark_safe(html)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields = [render_field(field, form, form_style, context) for field in self.fields] return render_to_string(self.template, { 'fields': ' '.join(fields), 'form_style': form_style, })
def render(self, form, form_style, context, **kwargs): try: feedback_text = '' maximum, minimum = self.maximum, self.minimum field_instance = form.fields[self.field] if isinstance(field_instance, forms.CharField): maximum = field_instance.max_length or maximum minimum = field_instance.min_length or minimum if maximum and isinstance(field_instance.widget, forms.Textarea): # django doesn't add it for some reason self.attrs.update({'maxlength': maximum}) if maximum: feedback_text = "/%s %s" % ( maximum, self.feedback_text or 'characters remaining') elif maximum is not None: self.attrs['data-maximum'] = maximum if minimum is not None: self.attrs['data-minimum'] = minimum if feedback_text: context.update({'wrapper_class': self.wrapper_class, 'feedback_text': feedback_text}) except KeyError: pass return render_field(self.field, form, form_style, context, template=self.template, attrs=self.attrs, **kwargs)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): """ Re-implement almost the same code from crispy_forms but using ``form`` instance to catch field errors. """ content = '' # accordion group needs the parent div id to set `data-parent` (I don't # know why). This needs to be a unique id if not self.css_id: self.css_id = "-".join(["accordion", text_type(randint(1000, 9999))]) # Active first 'AccordionItem' containing a field error if any, else # active first holder item self.open_target_group_for_form(form) for group in self.fields: group.data_parent = self.css_id group.item_has_errors = any([fieldname_error for fieldname_error in form.errors.keys() if fieldname_error in group]) content += render_field( group, form, form_style, context, template_pack=template_pack, **kwargs ) template = self.get_template_name(template_pack) context.update({'accordion': self, 'content': content}) return render_to_string(template, context.flatten())
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) return render_to_string(self.template, Context({'buttonholder': self, 'fields_output': html}))
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): if hasattr(form, 'parent') and hasattr(form.parent, 'can_delete') and form.parent.can_delete or 'DELETE' in form.fields: disable_delete_class = '' else: disable_delete_class = 'disable-delete' layout = [ HTML('<a href="javascript:;" class="%(delete_class)s">' '<i class="%(delete_icon_class)s"></i></a>' % { 'delete_class': ' '.join(filter(lambda x: x, (disable_delete_class, self.delete_class))), 'delete_icon_class': self.delete_icon_class, }), Hidden("%s-DELETE" % form.prefix, ''), ] if self.delete_div_class: layout = Div(*layout, css_class=self.delete_div_class) else: layout = Div(*layout) layout.tag = self.tag return render_field( layout, form, form_style, context, template_pack=template_pack )
def render(self, form, form_style, context, template_pack=None): template_pack = template_pack or get_template_pack() html = u"" errors = self._get_errors(form, self.fields) if len(errors) > 0: self.css_class += " has-error" for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) context.update( { "label_html": mark_safe(self.label_html), "field_html": mark_safe(html), "multifield": self, "error_list": errors, "help_bubble_text": self.help_bubble_text, } ) if not (self.field_class or self.label_class): return render_to_string(self.template, context) with edited_classes(context, self.label_class, self.field_class): rendered_view = render_to_string(self.template, context) return rendered_view
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): """ Re-implement almost the same code from crispy_forms but passing ``form`` instance to item ``render_link`` method. """ links, content = '', '' # accordion group needs the parent div id to set `data-parent` (I don't # know why). This needs to be a unique id if not self.css_id: self.css_id = "-".join(["tabsholder", text_type(randint(1000, 9999))]) for tab in self.fields: tab.active = False # Activate item self.open_target_group_for_form(form) for tab in self.fields: content += render_field( tab, form, form_style, context, template_pack=template_pack ) links += tab.render_link(form, template_pack) context.update({ 'tabs': self, 'links': links, 'content': content }) template = self.get_template_name(template_pack) return render_to_string(template, context.flatten())
def render(self, form, form_style, context, template_pack=None): template_pack = template_pack or get_template_pack() html = '' errors = self._get_errors(form, self.fields) if len(errors) > 0: self.css_class += " has-error" for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) context.update({ 'label_html': mark_safe(self.label_html), 'field_html': mark_safe(html), 'multifield': self, 'error_list': errors, 'help_bubble_text': self.help_bubble_text, }) context_dict = context.flatten() if not (self.field_class or self.label_class): return render_to_string(self.template, context_dict) with edited_classes(context, self.label_class, self.field_class): rendered_view = render_to_string(self.template, context_dict) return rendered_view
def render(self, form, *args, **kwargs): formset = getattr(form, self.formset_name) output = '' output += render_to_string(self.template, {'form_title': self.title, 'css': self.css, 'formset': formset, 'group_css': self.group_css}) for field in self.fields: output += render_field(field, form) return u''.join(output) # What is this for? Does this ensure unicode?
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields_output = u"" for field in self.fields: fields_output += render_field( field, form, form_style, context, self.field_template, self.label_class, layout_object=self, template_pack=template_pack, ) errors = self._get_errors(form, self.fields) if len(errors) > 0: self.css_class += " error" context.update( { "multifield": self, "fields_output": fields_output, "error_list": errors, "help_bubble_text": self.help_bubble_text, } ) return render_to_string(self.template, context)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): if hasattr(self, 'wrapper_class'): context['wrapper_class'] = self.wrapper_class html = '' for field in self.fields: html += render_field(field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): from crispy_forms.utils import render_field if not self.condition(form,context): return '' html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): field_list = [] for field in self.fields: field_list.append( render_field(field, form, form_style, context, template_pack=template_pack) ) return render_to_string(self.template, Context({'buttongroup': self, 'field_list': field_list}))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): fields = '' for field in self.fields: fields += render_field( field, form, form_style, context, template_pack=template_pack, **kwargs ) template = self.template % template_pack return render_to_string(template, {'div': self, 'fields': fields})
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): context['label_column'] = self.label_column context['input_column'] = self.input_column context['label_class'] = self.label_class html = '' for field in self.fields: html += render_field(field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields = '' for field in self.fields: try: fields += render_field(field, form, form_style, context, template_pack=template_pack) except Exception: # I really wish dcf had better exception handling continue return fields
def render(self, form, form_style, context, template_pack='bootstrap3', extra_context=None, **kwargs): classes = form.fields[self.field].widget.attrs.get('class', '') context.update( {'inputs': self.inputs, 'classes': classes.replace('form-control', '')}) if hasattr(self, 'wrapper_class'): context['wrapper_class'] = self.wrapper_class return render_field( self.field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack, extra_context=extra_context, **kwargs)
def render(self, form, form_style, context, template_pack=None): fields = '' for field in self.fields: fields += render_field(field, form, form_style, context, template_pack=template_pack) return fields
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields = '' for field in self.fields: fields += render_field(field, form, form_style, context, template_pack=template_pack) legend = '' if self.legend: legend = u'%s' % Template(unicode(self.legend)).render(context) return render_to_string(self.template, Context({'fieldset': self, 'legend': legend, 'fields': fields, 'form_style': form_style}))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, extra_context=None, **kwargs): # We first render the buttons field_template = self.field_template % template_pack buttons = ''.join( render_field(field, form, form_style, context, field_template, layout_object=self, template_pack=template_pack, **kwargs) for field in self.fields[1:]) extra_context = {'div': self, 'buttons': buttons} template = self.get_template_name(template_pack) if isinstance(self.fields[0], Field): # FieldWithButtons(Field('field_name'), StrictButton("go")) # We render the field passing its name and attributes return render_field(self.fields[0][0], form, form_style, context, template, attrs=self.fields[0].attrs, template_pack=template_pack, extra_context=extra_context, **kwargs) else: return render_field(self.fields[0], form, form_style, context, template, extra_context=extra_context, **kwargs)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): planned = render_field('status_%s_planned_date' % self.name, form, form_style, context, attrs=self.attrs, template=self.field_template, **kwargs) forecast = render_field('status_%s_forecast_date' % self.name, form, form_style, context, attrs=self.attrs, template=self.field_template, **kwargs) actual_attrs = self.attrs.copy() actual_attrs.update({'readonly': 'readonly'}) actual = render_field('status_%s_actual_date' % self.name, form, form_style, context, attrs=actual_attrs, template=self.field_template, **kwargs) return render_to_string( self.template, Context({ 'form_style': form_style, 'name': self.name, 'planned_field': planned, 'forecast_field': forecast, 'actual_field': actual, }))
def render_layout(self, form, context, template_pack=TEMPLATE_PACK): """ Returns safe html of the rendering of the layout """ form.rendered_fields = set() form.crispy_field_template = self.field_template # This renders the specifed Layout strictly html = self.layout.render(form, self.form_style, context, template_pack=template_pack) # Rendering some extra fields if specified if self.render_unmentioned_fields or self.render_hidden_fields or self.render_required_fields: fields = set(form.fields.keys()) left_fields_to_render = fields - form.rendered_fields for field in left_fields_to_render: if (self.render_unmentioned_fields or self.render_hidden_fields and form.fields[field].widget.is_hidden or self.render_required_fields and form.fields[field].widget.is_required): html += render_field(field, form, self.form_style, context, template_pack=template_pack) # If the user has Meta.fields defined, not included in the layout, # we suppose they need to be rendered if hasattr(form, 'Meta'): if hasattr(form.Meta, 'fields'): current_fields = set(getattr(form, 'fields', [])) meta_fields = set(getattr(form.Meta, 'fields')) fields_to_render = current_fields & meta_fields left_fields_to_render = fields_to_render - form.rendered_fields for field in left_fields_to_render: html += render_field(field, form, self.form_style, context) return mark_safe(html)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): html = "" for field in self.fields: html += render_field( field, form, form_style, context, template_pack=template_pack ) return html
def render(self, form, form_style, context, template_pack): fields = "" for field in self.fields: fields += render_field(field, form, form_style, context) return render_to_string(self.template, { "tag": self.element, "element": self, "fields": fields })
def render(self, form, form_style, context): fields = '' for field in self.fields: fields += render_field(field, form, form_style, context) return render_to_string( self.template, Context({ 'tag': self.element, 'elem': self, 'fields': fields }))
def render(self, form, *args, **kwargs): output = '<div class="%s">' % (self.css,) inner_form = getattr(form, self.form_name) if hasattr(inner_form, 'helper'): output += inner_form.helper.layout.render(inner_form) else: output = render_to_string(self.template, {'form': inner_form}) for field in self.fields: output += render_field(field, form) output += u'</div>' return u''.join(output) # What is this for? Does this ensure unicode?
def render(self, form, form_style, context): context.update({'extended_help': self.help}) output = u"" for field in self.fields: output += render_field(field, form, form_style, context, template=self.template, attrs=self.attrs) return output
def render(self, form, form_style, context, template_pack=None): template_pack = template_pack or get_template_pack() html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) context.update({ 'label_html': mark_safe(self.label_html), 'field_html': mark_safe(html), 'multifield': self, }) return render_to_string(self.template, context)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): html = u'' for field in self.fields: html += '<li>' + render_field( field, form, form_style, context, template_pack=template_pack) + '</li>' return render_to_string(self.template, { 'formactions': self, 'fields_output': html })
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack, **kwargs) extra_context = { 'formactions': self, 'fields_output': html, } if self.has_columns: extra_context.update(self.cols) return render_to_string(self.template, extra_context, context)
def render(self, form, form_style, context): if form.errors: self.css_class += " error" # We need to render fields using django-uni-form render_field so that MultiField can # hold other Layout objects inside itself fields_output = u'' self.bound_fields = [] for field in self.fields: fields_output += render_field(field, form, form_style, context, 'bootstrap/multifield.html', self.label_class, layout_object=self) return render_to_string(self.template, Context({'multifield': self, 'fields_output': fields_output}))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): from crispy_forms.utils import render_field if not self.condition(form, context): return '' html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): planned = render_field( 'status_%s_planned_date' % self.name, form, form_style, context, attrs=self.attrs, template=self.field_template, **kwargs) forecast = render_field( 'status_%s_forecast_date' % self.name, form, form_style, context, attrs=self.attrs, template=self.field_template, **kwargs) actual_attrs = self.attrs.copy() actual_attrs.update({'readonly': 'readonly'}) actual = render_field( 'status_%s_actual_date' % self.name, form, form_style, context, attrs=actual_attrs, template=self.field_template, **kwargs) return render_to_string( self.template, Context({ 'form_style': form_style, 'name': self.name, 'planned_field': planned, 'forecast_field': forecast, 'actual_field': actual, }))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): context['label_column'] = self.label_column context['input_column'] = self.input_column context['label_class'] = self.label_class html = '' template = self.get_template_name(template_pack) for field in self.fields: html += render_field(field, form, form_style, context, template=template, attrs=self.attrs, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=None): template_pack = template_pack or get_template_pack() html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) offsets = _get_offsets(context) return render_to_string(self.template, Context({ 'formactions': self, 'fields_output': html, 'offsets': offsets, 'field_class': context.get('field_class', '') }))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields = [ render_field(field, form, form_style, context) for field in self.fields ] return render_to_string( self.template, Context({ 'fields': ' '.join(fields), 'form_style': form_style, }))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, extra_context=None, **kwargs): extra_context = extra_context.copy( ) if extra_context is not None else {} template = self.get_template_name(template_pack) extra_context.update({ 'crispy_appended_text': self.appended_text, 'crispy_prepended_text': render_field(self.prepended_text, form, form_style, context, template, attrs=None, template_pack=template_pack, extra_context=None), 'input_size': self.input_size, 'active': getattr(self, "active", False) }) if hasattr(self, 'wrapper_class'): extra_context['wrapper_class'] = self.wrapper_class template = self.get_template_name(template_pack) return render_field(self.field, form, form_style, context, template=template, attrs=self.attrs, template_pack=template_pack, extra_context=extra_context, **kwargs)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): if callable(self.expr): value = self.expr(form, context) else: value = bool(self.expr) if value: return render_field( self.true_layout, form, form_style, context, template_pack=template_pack ) else: return render_field( self.false_layout, form, form_style, context, template_pack=template_pack )
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): classes = form.fields[self.field].widget.attrs.get('class', '') extra_context = { 'inputs': self.inputs, 'input_size': self.input_size, 'classes': classes.replace('form-control', '') } if hasattr(self, 'wrapper_class'): extra_context['wrapper_class'] = self.wrapper_class return render_field( self.field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack, extra_context=extra_context, **kwargs)
def render(self, form, form_style, context, template_pack=None): context.update({ 'field_text': self.text, }) if hasattr(self, 'wrapper_class'): context['wrapper_class'] = self.wrapper_class html = '' for field in self.fields: html += render_field(field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack='bootstrap'): classes = form.fields[self.field].widget.attrs['class'] context.update({ 'inputs': self.inputs, 'classes': classes.replace('form-control', '') }) return render_field(self.field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack)
def get_rendered_fields(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): return "".join( render_field(field, form, form_style, context, template_pack=template_pack, **kwargs) for field in self.fields)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): fields = '' for field in self.fields: fields += render_field(field, form, form_style, context, template_pack=template_pack) return render_to_string(self.template, Context({ 'div': self, 'fields': fields }))
def get_rendered_fields(self, form, form_style, context, template_pack=TEMPLATE_PACK, field_prefix=False, **kwargs): return ''.join( render_field(field, form, form_style, context, template_pack=template_pack, field_prefix=field_prefix, **kwargs) for field in self.fields)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): html = '' for field in self.fields: html += render_field(field, form, form_style, context, template=self.template, attrs=self.attrs) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): if hasattr(self, 'wrapper_class'): context['wrapper_class'] = self.wrapper_class if form.read_only: template = "%s/field.html" % TEMPLATE_PACK else: template = self.template html = '' for field in self.fields: html += render_field(field, form, form_style, context, template=template, attrs=self.attrs, template_pack=template_pack) return html
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): html = u'' for field in self.fields: html += render_field(field, form, form_style, context, template_pack=template_pack) return render_to_string( self.template, Context({ 'buttonholder': self, 'fields_output': html }))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): classes = form.fields[self.field].widget.attrs.get('class', '') context.update({ 'inputs': self.inputs, 'classes': classes.replace('form-control', '') }) if hasattr(self, 'wrapper_class'): context['wrapper_class'] = self.wrapper_class return render_field(self.field, form, form_style, context, template=self.template, attrs=self.attrs, template_pack=template_pack)
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): field_list = [] for field in self.fields: field_list.append( render_field(field, form, form_style, context, template_pack=template_pack)) return render_to_string( self.template, Context({ 'buttongroup': self, 'field_list': field_list }))
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): field_list = [] template = self.get_template_name(template_pack) for field in self.fields: field_list.append( render_field(field, form, form_style, context, template_pack=template_pack) ) buttons = render_to_string(template, { 'buttongroup': self, 'field_list': field_list, }) return buttons
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): content = "" for index, group in enumerate(self.fields, start=1): context["index"] = index context["parent"] = self.css_id content += render_field(group, form, form_style, context, template_pack=template_pack, **kwargs) template = self.get_template_name(template_pack) context.update({"accordion": self, "content": content}) return render_to_string(template, context.flatten())