我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用django.forms.BaseForm()。
def get_step_form(self, step=None): if step is None: step = self.steps.current attrs = self.get_form_list()[step] if type(attrs) in (list, tuple): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs, formfield_callback=self.admin_view.formfield_for_dbfield) elif type(attrs) is dict: if attrs.get('fields', None): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs['fields'], formfield_callback=self.admin_view.formfield_for_dbfield) if attrs.get('callback', None): callback = attrs['callback'] if callable(callback): return callback(self) elif hasattr(self.admin_view, str(callback)): return getattr(self.admin_view, str(callback))(self) elif issubclass(attrs, forms.BaseForm): return attrs return None
def get_step_form(self, step=None): if step is None: step = self.steps.current attrs = self.get_form_list()[step] if type(attrs) in (list, tuple): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs, formfield_callback=self.admin_view.formfield_for_dbfield) elif type(attrs) is dict: if attrs.get('fields', None): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs['fields'], formfield_callback=self.admin_view.formfield_for_dbfield) if attrs.get('callback', None): callback = attrs['callback'] if isinstance(callback, collections.Callable): return callback(self) elif hasattr(self.admin_view, str(callback)): return getattr(self.admin_view, str(callback))(self) elif issubclass(attrs, forms.BaseForm): return attrs return None
def get_step_form(self, step=None): if step is None: step = self.steps.current attrs = self.get_form_list()[step] if type(attrs) in (list, tuple): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs, formfield_callback=self.admin_view.formfield_for_dbfield) elif type(attrs) is dict: if attrs.get('fields', None): return modelform_factory(self.model, form=forms.ModelForm, fields=attrs['fields'], formfield_callback=self.admin_view.formfield_for_dbfield) if attrs.get('callback', None): callback = attrs['callback'] if callable(callback): return callback(self) elif hasattr(self.admin_view, str(callback)): return getattr(self.admin_view, str(callback))(self) elif isinstance(attrs, forms.BaseForm): return attrs return None
def _get_form_class(self): fields = self._get_form_fields() form_class = type( str('DateRangeForm'), (forms.BaseForm,), {'base_fields': fields} ) form_class.media = self._get_media() return form_class
def nice_errors(form, non_field_msg='General form errors'): nice_errors = ErrorDict() if isinstance(form, forms.BaseForm): for field, errors in form.errors.items(): if field == NON_FIELD_ERRORS: key = non_field_msg else: key = form.fields[field].label nice_errors[key] = errors return nice_errors
def wiki_form(context, form_obj): if not isinstance(form_obj, BaseForm): raise TypeError( "Error including form, it's not a form, it's a %s" % type(form_obj)) context.update({'form': form_obj}) return context # XXX html strong tag is hardcoded
def compute_form_list(cls, form_list, *args, **kwargs): computed_form_list = OrderedDict() assert len(form_list) > 0, 'at least one form is needed' # walk through the passed form list for i, form in enumerate(form_list): if isinstance(form, (list, tuple)): # if the element is a tuple, add the tuple to the new created # sorted dictionary. (step_name, form) = form if isinstance(form, dict): form_mapping = form computed_form_list[six.text_type(step_name)] = form_mapping elif isinstance(form, (list, tuple)): form_mapping = OrderedDict(form) computed_form_list[six.text_type(step_name)] = form_mapping elif issubclass(form, (forms.Form, forms.BaseForm, forms.BaseFormSet)): computed_form_list[six.text_type(step_name)] = form else: # if not, add the form with a zero based counter as unicode computed_form_list[six.text_type(i)] = form # walk through the new created list of forms for form in six.itervalues(computed_form_list): form_collection = [] if isinstance(form, dict): form_collection = form.values() elif issubclass(form, formsets.BaseFormSet): # if the element is based on BaseFormSet (FormSet/ModelFormSet) # we need to override the form variable. form = form.form form_collection = [form] for form in form_collection: # must test for BaseFormSet again in case form_collection # is a dict containing one. if issubclass(form, formsets.BaseFormSet): # if the element is based on BaseFormSet (FormSet or # ModelFormSet) we need to override the form variable. form = form.form # check if any form contains a FileField, if yes, we need a # file_storage added to the wizardview (by subclassing). for field in six.itervalues(form.base_fields): if (isinstance(field, forms.FileField) and not hasattr(cls, 'file_storage')): raise NoFileStorageConfigured( "You need to define 'file_storage' in your " "wizard view in order to handle file uploads.") return computed_form_list
def __init__(self, data=None, files=None, auto_id=None, prefix=None, initial=None, error_class=None, label_suffix=None, instance=None): """Constructor. Args (all optional and defaulting to None): data: dict of data values, typically from a POST request) files: dict of file upload values; Django 0.97 or later only auto_id, prefix: see Django documentation initial: dict of initial values error_class, label_suffix: see Django 0.97 or later documentation instance: Model instance to be used for additional initial values Except for initial and instance, these arguments are passed on to the forms.BaseForm constructor unchanged, but only if not None. Some arguments (files, error_class, label_suffix) are only supported by Django 0.97 or later. Leave these blank (i.e. None) when using Django 0.96. Their default values will be used with Django 0.97 or later even when they are explicitly set to None. """ opts = self._meta self.instance = instance object_data = {} if instance is not None: for name, prop in instance.properties().iteritems(): if opts.fields and name not in opts.fields: continue if opts.exclude and name in opts.exclude: continue object_data[name] = prop.get_value_for_form(instance) if initial is not None: object_data.update(initial) kwargs = dict(data=data, files=files, auto_id=auto_id, prefix=prefix, initial=object_data, error_class=error_class, label_suffix=label_suffix) kwargs = dict((name, value) for name, value in kwargs.iteritems() if value is not None) super(BaseModelForm, self).__init__(**kwargs)