我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用django.forms.forms.BoundField()。
def get_form_i18n_lines(form_instance): country_code = form_instance.i18n_country_code try: fields_order = i18naddress.get_field_order( {'country_code': country_code}) except ValueError: fields_order = i18naddress.get_field_order({}) field_mapping = dict(form_instance.I18N_MAPPING) def _convert_to_bound_fields(form, i18n_field_names): bound_fields = [] for field_name in i18n_field_names: local_fields = field_mapping[field_name] for local_name in local_fields: local_field = form_instance.fields[local_name] bound_field = BoundField(form, local_field, local_name) bound_fields.append(bound_field) return bound_fields if fields_order: return [_convert_to_bound_fields(form_instance, line) for line in fields_order]
def render(element, markup_classes): if isinstance(element, BoundField): add_input_classes(element) template = get_template("bootstrap/field.html") context = Context({'field': element, 'classes': markup_classes, 'form': element.form}) else: has_management = getattr(element, 'management_form', None) if has_management: for form in element.forms: for field in form.visible_fields(): add_input_classes(field) template = get_template("bootstrap/formset.html") context = Context({'formset': element, 'classes': markup_classes}) else: for field in element.visible_fields(): add_input_classes(field) template = get_template("bootstrap/form.html") context = Context({'form': element, 'classes': markup_classes}) return template.render(context)
def patch(): from django.forms import forms, formsets, util, widgets # Add __html__ methods to these classes: classes = [ forms.BaseForm, forms.BoundField, formsets.BaseFormSet, util.ErrorDict, util.ErrorList, widgets.Media, widgets.RadioFieldRenderer, ] try: classes.append(widgets.RadioChoiceInput) except AttributeError: classes.append(widgets.RadioInput) for cls in classes: if not hasattr(cls, '__html__'): cls.__html__ = __html__
def test_crispy_addon(self): test_form = TestForm() field_instance = test_form.fields['email'] bound_field = BoundField(test_form, field_instance, 'email') if self.current_template_pack == 'bootstrap': # prepend tests self.assertIn("input-prepend", crispy_addon(bound_field, prepend="Work")) self.assertNotIn("input-append", crispy_addon(bound_field, prepend="Work")) # append tests self.assertNotIn("input-prepend", crispy_addon(bound_field, append="Primary")) self.assertIn("input-append", crispy_addon(bound_field, append="Secondary")) # prepend and append tests self.assertIn("input-append", crispy_addon(bound_field, prepend="Work", append="Primary")) self.assertIn("input-prepend", crispy_addon(bound_field, prepend="Work", append="Secondary")) elif self.current_template_pack == 'bootsrap3': self.assertIn("input-group-addon", crispy_addon(bound_field, prepend="Work", append="Primary")) self.assertIn("input-group-addon", crispy_addon(bound_field, prepend="Work", append="Secondary")) # errors with self.assertRaises(TypeError): crispy_addon() crispy_addon(bound_field)
def as_crispy_field(field, template_pack=TEMPLATE_PACK): """ Renders a form field like a django-crispy-forms field:: {% load crispy_forms_tags %} {{ form.field|as_crispy_field }} or:: {{ form.field|as_crispy_field:"bootstrap" }} """ if not isinstance(field, forms.BoundField) and DEBUG: raise CrispyError('|as_crispy_field got passed an invalid or inexistent field') template = get_template('%s/field.html' % template_pack) c = Context({'field': field, 'form_show_errors': True, 'form_show_labels': True}) return template.render(c)
def field_(self, name): """ Get a form field starting with _. Taken near directly from Djano > forms. Returns a BoundField with the given name. """ try: field = self.fields[name] except KeyError: raise KeyError( "Key %r not found in '%s'" % (name, self.__class__.__name__)) return BoundField(self, field, name)
def bootstrap(element): if isinstance(element, BoundField): tpl = get_template("bootstrap/field.html") context = Context({'field': element}) else: has_management = getattr(element, 'management_form', None) if has_management: tpl = get_template("bootstrap/formset.html") context = Context({'formset': element}) else: tpl = get_template("bootstrap/form.html") context = Context({'form': element}) return tpl.render(context)
def test_crispy_field_and_class_converters(self): if hasattr(settings, 'CRISPY_CLASS_CONVERTERS'): template = loader.get_template_from_string(u""" {% load crispy_forms_field %} {% crispy_field testField 'class' 'error' %} """) test_form = TestForm() field_instance = test_form.fields['email'] bound_field = BoundField(test_form, field_instance, 'email') c = Context({'testField': bound_field}) html = template.render(c) self.assertTrue('error' in html) self.assertTrue('inputtext' in html)
def is_checkbox(value): if not isinstance(value, BoundField): return False return isinstance(value.field.widget, CheckboxInput)
def is_radio(value): if not isinstance(value, BoundField): return False return isinstance(value.field.widget, RadioSelect)
def __init__(self, field, *args, **kwargs): if not isinstance(field, BoundField): raise BootstrapError('Parameter "field" should contain a valid Django BoundField.') self.field = field super(FieldRenderer, self).__init__(*args, **kwargs) self.widget = field.field.widget self.is_multi_widget = isinstance(field.field.widget, MultiWidget) self.initial_attrs = self.widget.attrs.copy() self.field_help = text_value(mark_safe(field.help_text)) if self.show_help and field.help_text else '' self.field_errors = [conditional_escape(text_value(error)) for error in field.errors] if 'placeholder' in kwargs: # Find the placeholder in kwargs, even if it's empty self.placeholder = kwargs['placeholder'] elif get_bootstrap_setting('set_placeholder'): # If not found, see if we set the label self.placeholder = field.label else: # Or just set it to empty self.placeholder = '' if self.placeholder: self.placeholder = text_value(mark_safe(self.placeholder)) self.addon_before = kwargs.get('addon_before', self.widget.attrs.pop('addon_before', '')) self.addon_after = kwargs.get('addon_after', self.widget.attrs.pop('addon_after', '')) self.addon_before_class = kwargs.get('addon_before_class', self.widget.attrs.pop('addon_before_class', 'input-group-addon')) self.addon_after_class = kwargs.get('addon_after_class', self.widget.attrs.pop('addon_after_class', 'input-group-addon')) # These are set in Django or in the global BOOTSTRAP4 settings, and # they can be overwritten in the template error_css_class = kwargs.get('error_css_class', None) required_css_class = kwargs.get('required_css_class', None) bound_css_class = kwargs.get('bound_css_class', None) if error_css_class is not None: self.error_css_class = error_css_class else: self.error_css_class = getattr( field.form, 'error_css_class', get_bootstrap_setting('error_css_class') ) if required_css_class is not None: self.required_css_class = required_css_class else: self.required_css_class = getattr( field.form, 'required_css_class', get_bootstrap_setting('required_css_class') ) if bound_css_class is not None: self.success_css_class = bound_css_class else: self.success_css_class = getattr( field.form, 'bound_css_class', get_bootstrap_setting('success_css_class') ) # If the form is marked as form.empty_permitted, do not set required class if self.field.form.empty_permitted: self.required_css_class = ''
def __init__(self, field, *args, **kwargs): if not isinstance(field, BoundField): raise BootstrapError('Parameter "field" should contain a valid Django BoundField.') self.field = field super(FieldRenderer, self).__init__(*args, **kwargs) self.widget = field.field.widget self.is_multi_widget = isinstance(field.field.widget, MultiWidget) self.initial_attrs = self.widget.attrs.copy() self.field_help = text_value(mark_safe(field.help_text)) if self.show_help and field.help_text else '' self.field_errors = [conditional_escape(text_value(error)) for error in field.errors] if 'placeholder' in kwargs: # Find the placeholder in kwargs, even if it's empty self.placeholder = kwargs['placeholder'] elif get_bootstrap_setting('set_placeholder'): # If not found, see if we set the label self.placeholder = field.label else: # Or just set it to empty self.placeholder = '' self.addon_before = kwargs.get('addon_before', self.widget.attrs.pop('addon_before', '')) self.addon_after = kwargs.get('addon_after', self.widget.attrs.pop('addon_after', '')) self.addon_before_class = kwargs.get('addon_before_class', self.widget.attrs.pop('addon_before_class', 'input-group-addon')) self.addon_after_class = kwargs.get('addon_after_class', self.widget.attrs.pop('addon_after_class', 'input-group-addon')) # These are set in Django or in the global BOOTSTRAP4 settings, and # they can be overwritten in the template error_css_class = kwargs.get('error_css_class', None) required_css_class = kwargs.get('required_css_class', None) bound_css_class = kwargs.get('bound_css_class', None) if error_css_class is not None: self.error_css_class = error_css_class else: self.error_css_class = getattr( field.form, 'error_css_class', get_bootstrap_setting('error_css_class') ) if required_css_class is not None: self.required_css_class = required_css_class else: self.required_css_class = getattr( field.form, 'required_css_class', get_bootstrap_setting('required_css_class') ) if bound_css_class is not None: self.success_css_class = bound_css_class else: self.success_css_class = getattr( field.form, 'bound_css_class', get_bootstrap_setting('success_css_class') ) # If the form is marked as form.empty_permitted, do not set required class if self.field.form.empty_permitted: self.required_css_class = ''
def __init__(self, field, *args, **kwargs): if not isinstance(field, BoundField): raise BootstrapError('Parameter "field" should contain a valid Django BoundField.') self.field = field super(FieldRenderer, self).__init__(*args, **kwargs) self.widget = field.field.widget self.is_multi_widget = isinstance(field.field.widget, MultiWidget) self.initial_attrs = self.widget.attrs.copy() self.field_help = text_value(mark_safe(field.help_text)) if self.show_help and field.help_text else '' self.field_errors = [conditional_escape(text_value(error)) for error in field.errors] if get_bootstrap_setting('set_placeholder'): self.placeholder = field.label else: self.placeholder = '' self.addon_before = kwargs.get('addon_before', self.widget.attrs.pop('addon_before', '')) self.addon_after = kwargs.get('addon_after', self.widget.attrs.pop('addon_after', '')) # These are set in Django or in the global BOOTSTRAP3 settings, and # they can be overwritten in the template error_css_class = kwargs.get('error_css_class', None) required_css_class = kwargs.get('required_css_class', None) bound_css_class = kwargs.get('bound_css_class', None) if error_css_class is not None: self.error_css_class = error_css_class else: self.error_css_class = getattr( field.form, 'error_css_class', get_bootstrap_setting('error_css_class')) if required_css_class is not None: self.required_css_class = required_css_class else: self.required_css_class = getattr( field.form, 'required_css_class', get_bootstrap_setting('required_css_class')) if bound_css_class is not None: self.success_css_class = bound_css_class else: self.success_css_class = getattr( field.form, 'bound_css_class', get_bootstrap_setting('success_css_class')) # Handle form.empty_permitted if self.field.form.empty_permitted: self.set_required = False self.required_css_class = '' self.set_disabled = kwargs.get('set_disabled', False)