def get_field_tuple(name, form_or_model): """Returns a tuple for the field, of given instance, identified by "name". Instance could be a model instance, a form instance or any arbitrary object. The returned tuple is in the form: (label, attrs, value) """ name, sep, suffix = name.partition(':') label = "" value = "" td_attrs = {} field_list = get_fields(form_or_model) field = None if name in field_list: field = field_list[name] elif hasattr(form_or_model, name): field = getattr(form_or_model, name) if hasattr(field, 'short_description'): name = field.short_description if isinstance(field, models.Field): label = '%s:' % field.verbose_name value = '%s' % field_to_string(field, form_or_model) elif isinstance(field, forms.Field): bf = BoundField(form_or_model, field, name) label = '%s' % bf.label_tag() value = '%s' % bf if bf.help_text: value += '<br/><span title="%(help_text)s" class="helptext helppopup">%(help_text)s</span>' % { "help_text": '%s' % bf.help_text } errors = bf.errors if errors: value += '<br/>\n<ul class="errorlist">\n' for error in errors: value += '\t<li>%s</li>\n' % error value += '</ul>\n' css_classes = bf.css_classes() if css_classes: td_attrs['class'] = css_classes else: name = _(pretty_name(name).lower()) label = '%s:' % name.capitalize() value = field() if callable(field) else field firstcap_label = label[:1].upper() + label[1:] if suffix: value += " " + suffix return mark_safe(firstcap_label), flatatt(td_attrs), mark_safe(value)
def uka_form_row_stacked(element, errors='', extra_classes=''): label = BoundField.label_tag(element, "", {'class': 'uk-form-label'}, label_suffix='') if errors: extra_classes = f'{extra_classes} uk-form-danger uk-clearfix' help_text = f'<div class="uk-text-muted uk-margin-small-top"><span uk-icon="icon: comment"></span> {element.help_text}</div>' \ if element.help_text else '' original_classes = element.field.widget.attrs.get('class', '') applied_classes = 'uk-margin-small-top uk-margin-small-bottom' if element.field.widget.__class__.__name__ == "Select": applied_classes = f'{original_classes} uk-select' elif element.field.widget.__class__.__name__ == "RelatedFieldWidgetWrapper": applied_classes = f'{original_classes} uk-select' elif element.field.widget.__class__.__name__ in ["AdminTextareaWidget", "TextareaWidget"]: applied_classes = f'{original_classes} uk-textarea' else: applied_classes = original_classes # Trying some overrides ''' if element.field.__class__.__name__ in ['SplitDateTimeField', 'ReadOnlyPasswordHashField', 'ModelMultipleChoiceField', 'QuillField']: element = element.as_widget() elif element.field.__class__.__name__ == 'TextareaWidget': element = element.as_widget(attrs={'class': f'{applied_classes}'}) else: ''' element = element.as_widget(attrs={'class': f'uk-input uk-form-width-large {applied_classes} {extra_classes}'}) html_error = format_html(f'<div class="uk-text-danger uk-margin-top">{errors}</div>') html = format_html(f'<div class="uk-form-row">' \ f' <div>{label} {html_error}</div>' \ f' <div class="uk-form-controls" style="clear: both">{element}{help_text}</div>' \ f'</div>') return html