Django自定义表单字段初始数据
我无法理解如何在 django 视图中初始化自定义表单字段。
例如: http://djangosnippets.org/snippets/907/
from datetime import date, datetime
from calendar import monthrange
class CreditCardField(forms.IntegerField):
@staticmethod
def get_cc_type(number):
number = str(number)
#group checking by ascending length of number
if len(number) == 13:
if number[0] == "4":
return "Visa"
return "Unknown"
def clean(self, value):
if value and (len(value) < 13 or len(value) > 16):
raise forms.ValidationError("Please enter in a valid "+\
"credit card number.")
elif self.get_cc_type(value) not in ("Visa", "MasterCard",
"American Express"):
raise forms.ValidationError("Please enter in a Visa, "+\
"Master Card, or American Express credit card number.")
return super(CreditCardField, self).clean(value)
class CCExpWidget(forms.MultiWidget):
""" Widget containing two select boxes for selecting the month and year"""
def decompress(self, value):
return [value.month, value.year] if value else [None, None]
def format_output(self, rendered_widgets):
html = u' / '.join(rendered_widgets)
return u'<span style="white-space: nowrap">%s</span>' % html
class CCExpField(forms.MultiValueField):
EXP_MONTH = [(x, x) for x in xrange(1, 13)]
EXP_YEAR = [(x, x) for x in xrange(date.today().year,
date.today().year + 15)]
default_error_messages = {
'invalid_month': u'Enter a valid month.',
'invalid_year': u'Enter a valid year.',
}
def __init__(self, *args, **kwargs):
errors = self.default_error_messages.copy()
if 'error_messages' in kwargs:
errors.update(kwargs['error_messages'])
fields = (
forms.ChoiceField(choices=self.EXP_MONTH,
error_messages={'invalid': errors['invalid_month']}),
forms.ChoiceField(choices=self.EXP_YEAR,
error_messages={'invalid': errors['invalid_year']}),
)
super(CCExpField, self).__init__(fields, *args, **kwargs)
self.widget = CCExpWidget(widgets =
[fields[0].widget, fields[1].widget])
def clean(self, value):
exp = super(CCExpField, self).clean(value)
if date.today() > exp:
raise forms.ValidationError(
"The expiration date you entered is in the past.")
return exp
def compress(self, data_list):
if data_list:
if data_list[1] in forms.fields.EMPTY_VALUES:
error = self.error_messages['invalid_year']
raise forms.ValidationError(error)
if data_list[0] in forms.fields.EMPTY_VALUES:
error = self.error_messages['invalid_month']
raise forms.ValidationError(error)
year = int(data_list[1])
month = int(data_list[0])
# find last day of the month
day = monthrange(year, month)[1]
return date(year, month, day)
return None
class PaymentForm(forms.Form):
number = CreditCardField(required = True, label = "Card Number")
holder = forms.CharField(required = True, label = "Card Holder Name",
max_length = 60)
expiration = CCExpField(required = True, label = "Expiration")
ccv_number = forms.IntegerField(required = True, label = "CCV Number",
max_value = 9999, widget = forms.TextInput(attrs={'size': '4'}))
def __init__(self, *args, **kwargs):
self.payment_data = kwargs.pop('payment_data', None)
super(PaymentForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned = super(PaymentForm, self).clean()
if not self.errors:
result = self.process_payment()
if result and result[0] == 'Card declined':
raise forms.ValidationError('Your credit card was declined.')
elif result and result[0] == 'Processing error':
raise forms.ValidationError(
'We encountered the following error while processing '+\
'your credit card: '+result[1])
return cleaned
def process_payment(self):
if self.payment_data:
# don't process payment if payment_data wasn't set
datadict = self.cleaned_data
datadict.update(self.payment_data)
from virtualmerchant import VirtualMerchant
vmerchant = VirtualMerchant(datadict)
return vmerchant.process_virtualmerchant_payment()
在上面的付款表单示例中,如何将初始数据传递到 PaymentForm.expiration 字段?
我知道您可以这样做:
c = PaymentForm({'number':'1234567890', 'holder':'Bob Barker','ccv_number':'123'})
但是,如何将数据传递到自定义字段(例如此处实现的字段)?
I am having trouble understanding how to initialize a custom form field in a django view.
For example: http://djangosnippets.org/snippets/907/
from datetime import date, datetime
from calendar import monthrange
class CreditCardField(forms.IntegerField):
@staticmethod
def get_cc_type(number):
number = str(number)
#group checking by ascending length of number
if len(number) == 13:
if number[0] == "4":
return "Visa"
return "Unknown"
def clean(self, value):
if value and (len(value) < 13 or len(value) > 16):
raise forms.ValidationError("Please enter in a valid "+\
"credit card number.")
elif self.get_cc_type(value) not in ("Visa", "MasterCard",
"American Express"):
raise forms.ValidationError("Please enter in a Visa, "+\
"Master Card, or American Express credit card number.")
return super(CreditCardField, self).clean(value)
class CCExpWidget(forms.MultiWidget):
""" Widget containing two select boxes for selecting the month and year"""
def decompress(self, value):
return [value.month, value.year] if value else [None, None]
def format_output(self, rendered_widgets):
html = u' / '.join(rendered_widgets)
return u'<span style="white-space: nowrap">%s</span>' % html
class CCExpField(forms.MultiValueField):
EXP_MONTH = [(x, x) for x in xrange(1, 13)]
EXP_YEAR = [(x, x) for x in xrange(date.today().year,
date.today().year + 15)]
default_error_messages = {
'invalid_month': u'Enter a valid month.',
'invalid_year': u'Enter a valid year.',
}
def __init__(self, *args, **kwargs):
errors = self.default_error_messages.copy()
if 'error_messages' in kwargs:
errors.update(kwargs['error_messages'])
fields = (
forms.ChoiceField(choices=self.EXP_MONTH,
error_messages={'invalid': errors['invalid_month']}),
forms.ChoiceField(choices=self.EXP_YEAR,
error_messages={'invalid': errors['invalid_year']}),
)
super(CCExpField, self).__init__(fields, *args, **kwargs)
self.widget = CCExpWidget(widgets =
[fields[0].widget, fields[1].widget])
def clean(self, value):
exp = super(CCExpField, self).clean(value)
if date.today() > exp:
raise forms.ValidationError(
"The expiration date you entered is in the past.")
return exp
def compress(self, data_list):
if data_list:
if data_list[1] in forms.fields.EMPTY_VALUES:
error = self.error_messages['invalid_year']
raise forms.ValidationError(error)
if data_list[0] in forms.fields.EMPTY_VALUES:
error = self.error_messages['invalid_month']
raise forms.ValidationError(error)
year = int(data_list[1])
month = int(data_list[0])
# find last day of the month
day = monthrange(year, month)[1]
return date(year, month, day)
return None
class PaymentForm(forms.Form):
number = CreditCardField(required = True, label = "Card Number")
holder = forms.CharField(required = True, label = "Card Holder Name",
max_length = 60)
expiration = CCExpField(required = True, label = "Expiration")
ccv_number = forms.IntegerField(required = True, label = "CCV Number",
max_value = 9999, widget = forms.TextInput(attrs={'size': '4'}))
def __init__(self, *args, **kwargs):
self.payment_data = kwargs.pop('payment_data', None)
super(PaymentForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned = super(PaymentForm, self).clean()
if not self.errors:
result = self.process_payment()
if result and result[0] == 'Card declined':
raise forms.ValidationError('Your credit card was declined.')
elif result and result[0] == 'Processing error':
raise forms.ValidationError(
'We encountered the following error while processing '+\
'your credit card: '+result[1])
return cleaned
def process_payment(self):
if self.payment_data:
# don't process payment if payment_data wasn't set
datadict = self.cleaned_data
datadict.update(self.payment_data)
from virtualmerchant import VirtualMerchant
vmerchant = VirtualMerchant(datadict)
return vmerchant.process_virtualmerchant_payment()
In the above example payment form, how would you pass initial data to PaymentForm.expiration field?
I know you can do:
c = PaymentForm({'number':'1234567890', 'holder':'Bob Barker','ccv_number':'123'})
However, how do you pass data to a custom field such as the one implemented here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有字段都有一个 'initial' 属性,因此即使它是自定义字段
https://code.djangoproject.com/browser/django/trunk/django/forms/fields.py#L45
所以你应该能够覆盖构造函数:
并且在您看来您可以传递所需的数据:
All fields have an 'initial' attribute so you set that even if it's a custom field
https://code.djangoproject.com/browser/django/trunk/django/forms/fields.py#L45
so you should just be able to overwrite the constructor:
and in your view you can pass the required data: