我们从Python开源项目中,提取了以下46个代码示例,用于说明如何使用django.forms.widgets.TextInput()。
def test_edit_attachments_auto_device_name(self): volume = self.cinder_volumes.first() servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] volume.attachments = [{'id': volume.id, 'volume_id': volume.id, 'volume_name': volume.name, 'instance': servers[0], 'device': '', 'server_id': servers[0].id}] cinder.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume) api.nova.server_list(IsA(http.HttpRequest)).AndReturn([servers, False]) self.mox.ReplayAll() url = reverse('horizon:project:volumes:volumes:attach', args=[volume.id]) res = self.client.get(url) form = res.context['form'] self.assertIsInstance(form.fields['device'].widget, widgets.TextInput) self.assertFalse(form.fields['device'].required)
def __init__(self, attrs=None): color_widget = widgets.TextInput(attrs={ 'type': 'color', 'class': 'colorfield-preview input-small', }) input_widget = widgets.TextInput(attrs={ 'max_length': 7, 'placeholder': 'hex color', 'class': 'colorfield-hex input-small', 'pattern': '#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})', }) opacity_widget = widgets.TextInput(attrs={ 'type': 'number', 'placeholder': 'opacity', 'class': 'colorfield-opacity input-mini', 'step': '0.05', 'min': 0, 'max': 1, }) _widgets = (color_widget, input_widget, opacity_widget) super().__init__(_widgets, attrs)
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] values = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True) name = factory.make_string() html_widget = fromstring( '<root>' + widget.render(name, values) + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) widget_values = XPath('fieldset/input/@value')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names] self.assertEqual( [expected_names, labels, values], [widget_names, widget_labels, widget_values])
def test_DictCharWidget_value_from_datadict_values_from_data(self): # 'value_from_datadict' extracts the values corresponding to the # field as a dictionary. names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] name = factory.make_string() field_1_value = factory.make_string() field_2_value = factory.make_string() # Create a query string with the field2 before the field1 and another # (unknown) value. data = QueryDict( '%s_%s=%s&%s_%s=%s&%s=%s' % ( name, names[1], field_2_value, name, names[0], field_1_value, factory.make_string(), factory.make_string()) ) widget = DictCharWidget( [widgets.TextInput, widgets.TextInput], names, initials, labels) self.assertEqual( {names[0]: field_1_value, names[1]: field_2_value}, widget.value_from_datadict(data, None, name))
def test_DictCharWidget_renders_with_empty_string_as_input_data(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True) name = factory.make_string() html_widget = fromstring( '<root>' + widget.render(name, '') + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names] self.assertEqual( [expected_names, labels], [widget_names, widget_labels])
def test_edit_attachments(self): volume = self.cinder_volumes.first() servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] volume.attachments = [{'id': volume.id, 'volume_id': volume.id, 'volume_name': volume.name, 'instance': servers[0], 'device': '/dev/vdb', 'server_id': servers[0].id}] cinder.volume_get(IsA(http.HttpRequest), volume.id).AndReturn(volume) api.nova.server_list(IsA(http.HttpRequest)).AndReturn([servers, False]) self.mox.ReplayAll() url = reverse('horizon:project:volumes:volumes:attach', args=[volume.id]) res = self.client.get(url) msg = 'Volume %s on instance %s' % (volume.name, servers[0].name) self.assertContains(res, msg) # Asserting length of 2 accounts for the one instance option, # and the one 'Choose Instance' option. form = res.context['form'] self.assertEqual(len(form.fields['instance']._choices), 1) self.assertEqual(res.status_code, 200) self.assertIsInstance(form.fields['device'].widget, widgets.TextInput) self.assertFalse(form.fields['device'].required)
def test_launch_form_instance_device_name_showed(self): self._test_launch_form_instance_show_device_name( u'vda', widgets.TextInput, { 'name': 'device_name', 'value': 'vda', 'attrs': {'id': 'id_device_name'}} )
def as_text(self, attrs=None, **kwargs): """ Returns a string of HTML for representing this as an <input type="text">. """ return self.as_widget(TextInput(), attrs, **kwargs)
def __init__(self, *args, **kwargs): super(PackageForm, self).__init__(*args, **kwargs) self.fields['category'].help_text = package_help_text() self.fields['repo_url'].widget = TextInput(attrs={ 'placeholder': 'ex: https://github.com/steemit/steem' }) self.fields['description'].widget = Textarea(attrs={ "placeholder": "Write few sentences about this projects. What problem does it solve? Who is it for?" }) self.fields['contact_url'].widget = TextInput(attrs={ "placeholder": "Link to channel on steemit.chat, discord, slack, etc" })
def form_class_from_model(self): if self.model_name in self.model_form_fields: Form = modelform_factory(self.model, fields=self.model_form_fields[self.model_name], widgets={'user': widgets.TextInput}) else: Form = modelform_factory(self.model, exclude=self.excluded_fields, widgets={'user': widgets.TextInput}) return Form
def __init__(self, attrs=None): color_widget = widgets.TextInput(attrs={ 'type': 'color', 'class': 'colorfield-preview input-small', }) input_widget = widgets.TextInput(attrs={ 'max_length': 7, 'placeholder': 'hex color', 'class': 'colorfield-hex input-small', 'pattern': '#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})', }) _widgets = (color_widget, input_widget) super().__init__(_widgets, attrs)
def __init__(self, send_label, fieldsetup_id, email_attrs, code_attrs): self.send_label = send_label self.fieldsetup_id = fieldsetup_id widgets = (EmailInput(attrs=email_attrs), TextInput(attrs=code_attrs)) super(VerifiedEmailWidget, self).__init__(widgets)
def __init__(self, attrs=None): _widgets = ( widgets.TextInput(attrs=attrs), widgets.Select(attrs=attrs, choices=Event.DURATION_MULTIPLIER), ) super(UnitsAndDurationWidget, self).__init__(_widgets, attrs)
def __init__(self, *args, **kwargs): super(LoginForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "username", "class": "form-control"}) self.fields['password'].widget = widgets.PasswordInput( attrs={'placeholder': "password", "class": "form-control"})
def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "username", "class": "form-control"}) self.fields['email'].widget = widgets.EmailInput(attrs={'placeholder': "email", "class": "form-control"}) self.fields['password1'].widget = widgets.PasswordInput( attrs={'placeholder': "password", "class": "form-control"}) self.fields['password2'].widget = widgets.PasswordInput( attrs={'placeholder': "repeat password", "class": "form-control"})
def test_DictCharWidget_id_for_label_uses_first_fields_name(self): names = [factory.make_string()] initials = [] labels = [factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput], names, initials, labels) self.assertEqual( ' _%s' % names[0], widget.id_for_label(' '))
def test_DictCharWidget_renders_with_initial_when_no_value(self): """Widgets should use initial value if rendered without value.""" names = [factory.make_name()] initials = [factory.make_name()] labels = [factory.make_name()] mock_widget = Mock() mock_widget.configure_mock(**{"render.return_value": ""}) widget = DictCharWidget([mock_widget, widgets.TextInput], names, initials, labels, skip_check=True) widget.render('foo', []) self.assertThat( mock_widget.render, MockCalledOnceWith(ANY, initials[0], ANY))
def render(self, name, value, attrs=None): html_id = attrs and attrs.get('id', name) or name has_image_set = self.is_initial(value) is_required = self.is_required file_placeholder = ugettext('Select a picture from your local folder.') file_input = super().render(name, None, { 'id': html_id, 'class': 'form-control form-control-file' }) if has_image_set: file_name = basename(value.name) file_url = conditional_escape(value.url) else: file_name = "" file_url = "" text_input = widgets.TextInput().render('__noname__', file_name, { 'class': 'form-control form-control-file-dummy', 'placeholder': file_placeholder, 'tabindex': '-1', 'id': 'text-{}'.format(html_id) }) checkbox_id = self.clear_checkbox_id(name) checkbox_name = self.clear_checkbox_name(name) checkbox_input = widgets.CheckboxInput().render(checkbox_name, False, { 'id': checkbox_id, 'class': 'clear-image', 'data-upload-clear': html_id, }) context = { 'id': html_id, 'has_image_set': has_image_set, 'is_required': is_required, 'file_url': file_url, 'file_input': file_input, 'file_id': html_id + '-file', 'text_input': text_input, 'checkbox_input': checkbox_input, 'checkbox_id': checkbox_id } return loader.render_to_string( 'a4images/image_upload_widget.html', context )
def render(self, name, value, attrs=None): has_file_set = self.is_initial(value) is_required = self.is_required file_placeholder = ugettext('Select a file from your local folder.') file_input = super().render(name, None, { 'id': name, 'class': 'form-control form-control-file' }) if has_file_set: file_name = basename(value.name) file_url = conditional_escape(value.url) else: file_name = "" file_url = "" text_input = widgets.TextInput().render('__noname__', file_name, { 'class': 'form-control form-control-file-dummy', 'placeholder': file_placeholder }) checkbox_id = self.clear_checkbox_id(name) checkbox_name = self.clear_checkbox_name(name) checkbox_input = widgets.CheckboxInput().render(checkbox_name, False, { 'id': checkbox_id, 'class': 'clear-image', 'data-upload-clear': name, }) context = { 'name': name, 'has_image_set': has_file_set, 'is_required': is_required, 'file_url': file_url, 'file_input': file_input, 'file_id': name + '-file', 'text_input': text_input, 'checkbox_input': checkbox_input, 'checkbox_id': checkbox_id } return mark_safe( loader.render_to_string( 'euth_offlinephases/file_upload_widget.html', context ) )