我有一个模型表单,我想在其中将 required 属性设置为 True 以进行电子邮件验证
场地:-email
class RegisterMyBuisinessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = '/registermybuisness/'
Field('email', type='email')
self.helper.add_input(Submit('submit', 'Submit',css_class="btn c-theme-btn c-btn-square c-btn-bold c-btn-uppercase"))
super(RegisterMyBuisinessForm, self).__init__(*args, **kwargs)
class Meta:
model = RegistermyBusiness
fields = ['name','email', 'org_name', 'contact','business_description','store_address','message','store_landmark','business_type','city']
Run Code Online (Sandbox Code Playgroud)
我试过
self.fields['email'].required=True
Run Code Online (Sandbox Code Playgroud)
这导致类 RegisterMyBuisinessForm 没有字段错误
我有这样的清单
li = [
{1: {'amount': 255, 'date': '25-02-2017'}},
{2: {'amount': 25, 'date': '2-02-2017'}},
{3: {'amount': 38, 'date': '20-02-2017'}},
]
Run Code Online (Sandbox Code Playgroud)
我试图基于的升序和降序对数据进行排序'amount'.
我尝试了以下但我无法得到我想要的结果:
import operator
li.sort(key=operator.itemgetter('amount'))
Run Code Online (Sandbox Code Playgroud)
和
from operator import itemgetter
newlist = sorted(li, key=itemgetter('amount'))
Run Code Online (Sandbox Code Playgroud)
两者都引发异常KeyError: 'amount'.