本文整理汇总了Python中django.forms.CharField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.CharField方法的具体用法?Python forms.CharField怎么用?Python forms.CharField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms
的用法示例。
在下文中一共展示了forms.CharField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_student
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def new_student(request, userid):
person = get_object_or_404(Person, find_userid_or_emplid(userid))
semester = Semester.next_starting()
semesterconfig = SemesterConfig.get_config(request.units, semester)
student = get_object_or_404(Person, find_userid_or_emplid(userid))
initial = {'person': student.emplid, 'start_date': semesterconfig.start_date(), 'end_date': semesterconfig.end_date(), 'hours': 80 }
scholarship_choices, hiring_faculty_choices, unit_choices, project_choices, account_choices, program_choices = \
_appointment_defaults(request.units, emplid=student.emplid)
gss = GradStudent.objects.filter(person=student)
if gss:
gradstudent = gss[0]
initial['sin'] = gradstudent.person.sin()
raform = RAForm(initial=initial)
raform.fields['person'] = forms.CharField(widget=forms.HiddenInput())
raform.fields['scholarship'].choices = scholarship_choices
raform.fields['hiring_faculty'].choices = hiring_faculty_choices
raform.fields['unit'].choices = unit_choices
raform.fields['project'].choices = project_choices
raform.fields['account'].choices = account_choices
raform.fields['program'].choices = program_choices
return render(request, 'ra/new.html', { 'raform': raform, 'person': person })
#Edit RA Appointment
示例2: make_entry_field
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def make_entry_field(self, fieldsubmission=None):
self.min_length = 0
self.max_length = 0
if self.config['min_length'] and int(self.config['min_length']) > 0:
self.min_length = int(self.config['min_length'])
if self.config['max_length'] and int(self.config['max_length']) > 0:
self.max_length = int(self.config['max_length'])
c = forms.CharField(required=self.config['required'],
widget=forms.Textarea(attrs={'cols': '60', 'rows': self.config.get('rows', '3')}),
label=self.config['label'],
help_text=self.config['help_text'],
min_length=self.min_length,
max_length=self.max_length)
if fieldsubmission:
c.initial = fieldsubmission.data['info']
return c
示例3: add_extra_questions
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def add_extra_questions(self, event):
if 'extra_questions' in event.config and len(event.config['extra_questions']) > 0:
extra_fields = []
for question in event.config['extra_questions']:
extra_fields.append(question)
if 'extra_questions' in self.instance.config and question in self.instance.config['extra_questions']:
self.fields[question] = \
forms.CharField(label=question,widget=forms.Textarea,
initial=self.instance.config['extra_questions'][question])
else:
self.fields[question] = forms.CharField(label=question, widget=forms.Textarea)
# We want to move the extra questions *before* the waivers, for neatness' sake.
field_list = (self._meta.fields)
# We got the original list of fields, add our extra questions in the spot before two before last.
field_list[len(field_list) - 2:len(field_list) - 2] = extra_fields
# Re-order the fields to that list.
self.order_fields(field_list)
示例4: test_extra_fields
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def test_extra_fields(self):
class FooModelService(ModelService):
two = forms.CharField()
class Meta:
model = FooModel
fields = '__all__'
def process(self):
pass
f = FooModelService()
field_names = list(six.iterkeys(f.fields))
self.assertEqual(2, len(field_names))
self.assertEqual('one', field_names[0])
self.assertEqual('two', field_names[1])
示例5: test_model_init_params
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def test_model_init_params(self):
"""Model __init__ gets all fields as params."""
lines = []
docstrings._add_model_fields_as_params(self.app, SimpleModel, lines)
self.assertEqual(
lines,
[
":param id: Id",
":type id: AutoField",
":param user: User",
":type user: ForeignKey to :class:`~django.contrib.auth.models.User`",
":param user2: User2",
":type user2: ForeignKey to"
" :class:`~sphinxcontrib_django.tests.test_docstrings.User2`",
":param user3: User3",
":type user3: ForeignKey to :class:`~django.contrib.auth.models.User`",
":param dummy_field: Dummy field",
":type dummy_field: CharField",
],
)
示例6: test_add_form_fields
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def test_add_form_fields(self):
"""Form fields should be mentioned."""
lines = []
docstrings._add_form_fields(SimpleForm, lines)
self.assertEqual(
lines,
[
"**Form fields:**",
"",
"* ``user``: User (:class:`~django.forms.models.ModelChoiceField`)",
"* ``user2``: User2 (:class:`~django.forms.models.ModelChoiceField`)",
"* ``user3``: User3 (:class:`~django.forms.models.ModelChoiceField`)",
"* ``test1``: Test1 (:class:`~django.forms.fields.CharField`)",
"* ``test2``: Test2 (:class:`~django.forms.fields.CharField`)",
],
)
示例7: get_field_kind
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def get_field_kind(field):
if field['kind'] == 'string':
return forms.CharField(max_length=255, required=False)
elif field['kind'] == 'email':
return forms.EmailField(max_length=255, required=False)
elif field['kind'] == 'integer':
return forms.IntegerField(required=False)
elif field['kind'] == 'boolean':
return forms.BooleanField(required=False)
elif field['kind'] == 'text':
return forms.CharField(widget=forms.Textarea(), required=False)
elif field['kind'] == 'choice':
return forms.ChoiceField(choices=map(lambda c: (c,c), field['choices']))
elif field['kind'] == 'timezones':
return TimezoneField()
elif field['kind'] == 'authentication':
return SwitchField('user', 'service', required=True)
elif field['kind'] == 'json':
return JsonField(required=False)
elif field['kind'] == 'integer_list':
return CommaSeparatedIntegerField(required=False)
elif field['kind'] == 'string_list':
return CommaSeparatedCharField(required=False)
else:
return forms.CharField(max_length=255, required=False)
示例8: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def __init__(self, *args, **kwargs):
fields = []
defaults = {"widget": self.widget, "max_length": kwargs.pop("max_length", None)}
self.locales = kwargs.pop("locales", settings.LANGUAGES)
self.one_required = kwargs.get("required", True)
require_all_fields = kwargs.pop("require_all_fields", False)
kwargs["required"] = False
kwargs["widget"] = kwargs["widget"](
locales=self.locales, field=self, **kwargs.pop("widget_kwargs", {})
)
defaults.update(**kwargs)
for lngcode, _ in self.locales:
defaults["label"] = "%s (%s)" % (defaults.get("label"), lngcode)
field = forms.CharField(**defaults)
field.locale = lngcode
fields.append(field)
super().__init__(fields=fields, require_all_fields=False, *args, **kwargs)
self.require_all_fields = require_all_fields
示例9: test_formfield_callback
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def test_formfield_callback(self):
def formfield_for_dbfield(db_field, **kwargs):
# a particularly stupid formfield_callback that just uses Textarea for everything
return CharField(widget=Textarea, **kwargs)
class BandFormWithFFC(ClusterForm):
formfield_callback = formfield_for_dbfield
class Meta:
model = Band
fields = ['name']
form = BandFormWithFFC()
self.assertEqual(Textarea, type(form['name'].field.widget))
self.assertEqual(Textarea, type(form.formsets['members'].forms[0]['name'].field.widget))
示例10: build_default_field
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def build_default_field(self, field, model):
choices = getattr(field, "choices", None)
if choices:
return forms.ChoiceField(
required=False,
label=_("Default value"),
choices=[(None, "-----------")] + list(choices),
)
if not (model is Member and field.name == "number"):
if isinstance(field, models.CharField):
return forms.CharField(required=False, label=_("Default value"))
elif isinstance(field, models.DecimalField):
return forms.DecimalField(
required=False,
label=_("Default value"),
max_digits=field.max_digits,
decimal_places=field.decimal_places,
)
elif isinstance(field, models.DateField):
return forms.CharField(required=False, label=_("Other/fixed date"))
示例11: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def __init__(self, *args, **kwargs):
active = kwargs.pop('active', None)
request = kwargs.pop('request', None)
super(AdminUserForm, self).__init__(*args, **kwargs)
if not kwargs.get('instance', None):
self.fields['is_active'].initial = True
if active == 'add':
self.fields['password_1'] = forms.CharField(widget=forms.PasswordInput, label="Password")
self.fields['password_2'] = forms.CharField(widget=forms.PasswordInput, label="Repeat password")
if request and not request.user.is_admin:
self.fields.pop('is_staff', None)
self.fields.pop('is_admin', None)
if request and not request.user.is_superuser:
self.fields.pop('is_superuser')
示例12: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def __init__(self, *args, **kwargs):
components = kwargs.get('components')
del(kwargs['components'])
super(GsxComponentForm, self).__init__(*args, **kwargs)
if len(components):
components = json.loads(components)
for k, v in components.items():
self.fields[k] = forms.CharField(label=k, required=True, initial=v)
示例13: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def __init__(self, *args, **kwargs):
fields = (
forms.CharField(label=_('place')),
forms.DecimalField(label=_('Latitude')),
forms.DecimalField(label=_('Longitude')),
)
if 'initial' in kwargs:
kwargs['initial'] = Places(*kwargs['initial'].split(','))
self.widget = PlacesWidget()
super(PlacesField, self).__init__(fields, **kwargs)
示例14: setUp
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def setUp(self):
class MockForm(forms.Form):
text_field = forms.CharField()
date_field = forms.DateField()
self.form_class = MockForm
self.form = self.form_class()
示例15: __init__
# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import CharField [as 别名]
def __init__(self, event=None, *args, **kwargs):
super(DonationEntryForm, self).__init__(*args, **kwargs)
minDonationAmount = (
event.minimumdonation if event is not None else Decimal('1.00')
)
self.fields['amount'] = forms.DecimalField(
decimal_places=2,
min_value=minDonationAmount,
max_value=Decimal('100000'),
label='Donation Amount (min ${0})'.format(minDonationAmount),
widget=tracker.widgets.NumberInput(
attrs={
'id': 'iDonationAmount',
'min': str(minDonationAmount),
'step': '0.01',
}
),
required=True,
)
self.fields['comment'] = forms.CharField(widget=forms.Textarea, required=False)
self.fields['requestedvisibility'] = forms.ChoiceField(
initial='CURR',
choices=models.Donation._meta.get_field('requestedvisibility').choices,
label='Name Visibility',
)
self.fields['requestedalias'] = forms.CharField(
max_length=32, label='Preferred Alias', required=False
)
self.fields['requestedemail'] = forms.EmailField(
max_length=128, label='Preferred Email', required=False
)
self.fields['requestedsolicitemail'] = forms.ChoiceField(
initial='CURR',
choices=models.Donation._meta.get_field('requestedsolicitemail').choices,
label='Charity Email Opt In',
)