本文整理汇总了Python中django.forms.Form.disable_fields方法的典型用法代码示例。如果您正苦于以下问题:Python Form.disable_fields方法的具体用法?Python Form.disable_fields怎么用?Python Form.disable_fields使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.Form
的用法示例。
在下文中一共展示了Form.disable_fields方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: billingcycle_edit
# 需要导入模块: from django.forms import Form [as 别名]
# 或者: from django.forms.Form import disable_fields [as 别名]
def billingcycle_edit(request, id, template_name='membership/entity_edit.html'):
cycle = get_object_or_404(BillingCycle, id=id)
class Form(ModelForm):
is_paid_forced = False
class Meta:
model = BillingCycle
exclude = ('membership', 'start', 'end', 'sum', 'reference_number')
def disable_fields(self):
self.fields['is_paid'].required = False
if cycle.amount_paid() >= cycle.sum and cycle.is_paid:
self.fields['is_paid'].widget.attrs['disabled'] = 'disabled'
self.is_paid_forced = True
def clean_is_paid(self):
if self.is_paid_forced:
return cycle.is_paid
else:
return self.cleaned_data['is_paid']
before = cycle.__dict__.copy() # Otherwise save() (or valid?) will change the dict, needs to be here
if request.method == 'POST':
form = Form(request.POST, instance=cycle)
form.disable_fields()
if form.is_valid():
form.save()
after = cycle.__dict__
log_change(cycle, request.user, before, after)
messages.success(request, unicode(_("Changes to billing cycle %s saved.") % cycle))
return redirect('billingcycle_edit', id) # form stays as POST otherwise if someone refreshes
else:
messages.error(request, unicode(_("Changes to bill %s not saved.") % cycle))
else:
form = Form(instance=cycle)
form.disable_fields()
logentries = bake_log_entries(cycle.logs.all())
return render_to_response(template_name, {'form': form, 'cycle': cycle,
'logentries': logentries},
context_instance=RequestContext(request))
示例2: membership_edit
# 需要导入模块: from django.forms import Form [as 别名]
# 或者: from django.forms.Form import disable_fields [as 别名]
def membership_edit(request, id, template_name='membership/membership_edit.html'):
membership = get_object_or_404(Membership, id=id)
class Form(ModelForm):
class Meta:
model = Membership
exclude = ('person', 'billing_contact', 'tech_contact', 'organization')
def clean_status(self):
return membership.status
def clean_approved(self):
return membership.approved
def disable_fields(self):
self.fields['status'].required = False
self.fields['status'].widget.attrs['disabled'] = 'disabled'
self.fields['approved'].required = False
self.fields['approved'].widget.attrs['disabled'] = 'disabled'
if request.method == 'POST':
if not request.user.has_perm('membership.manage_members'):
return HttpResponseForbidden(_("Permission manage required"))
form = Form(request.POST, instance=membership)
before = membership.__dict__.copy()
form.disable_fields()
if form.is_valid():
form.save()
after = membership.__dict__
log_change(membership, request.user, before, after)
return redirect('membership_edit', id) # form stays as POST otherwise if someone refreshes
else:
form = Form(instance=membership)
form.disable_fields()
# Pretty print log entries for template
logentries = bake_log_entries(membership.logs.all())
return render_to_response(template_name, {'form': form,
'membership': membership, 'logentries': logentries},
context_instance=RequestContext(request))
示例3: payment_edit
# 需要导入模块: from django.forms import Form [as 别名]
# 或者: from django.forms.Form import disable_fields [as 别名]
def payment_edit(request, id, template_name='membership/entity_edit.html'):
payment = get_object_or_404(Payment, id=id)
class SpeciallyLabeledModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return u"%s, %s" % (obj.membership, unicode(obj))
class Form(ModelForm):
class Meta:
model = Payment
#exclude = ('billingcycle')
billingcycle = CharField(widget=HiddenInput(), required=False)
#billingcycle = CharField(required=False)
message = CharField(widget=Textarea(attrs={'rows': 5, 'cols': 60}))
def disable_fields(self):
if payment.billingcycle:
self.fields['ignore'].required = False
self.fields['ignore'].widget.attrs['disabled'] = 'disabled'
self.fields['billingcycle'].required = False
self.fields['billingcycle'].widget.attrs['disabled'] = 'disabled'
self.fields['reference_number'].required = False
self.fields['reference_number'].widget.attrs['disabled'] = 'disabled'
self.fields['message'].required = False
self.fields['message'].widget.attrs['disabled'] = 'disabled'
self.fields['transaction_id'].required = False
self.fields['transaction_id'].widget.attrs['disabled'] = 'disabled'
self.fields['payment_day'].required = False
self.fields['payment_day'].widget.attrs['disabled'] = 'disabled'
self.fields['amount'].required = False
self.fields['amount'].widget.attrs['disabled'] = 'disabled'
self.fields['type'].required = False
self.fields['type'].widget.attrs['disabled'] = 'disabled'
self.fields['payer_name'].required = False
self.fields['payer_name'].widget.attrs['disabled'] = 'disabled'
self.fields['comment'].required = False
def clean_ignore(self):
if payment.billingcycle:
return False
else:
return self.cleaned_data['ignore']
def clean_billingcycle(self):
return payment.billingcycle
def clean_reference_number(self):
return payment.reference_number
def clean_message(self):
return payment.message
def clean_transaction_id(self):
return payment.transaction_id
def clean_payment_day(self):
return payment.payment_day
def clean_amount(self):
return payment.amount
def clean_type(self):
return payment.type
def clean_payer_name(self):
return payment.payer_name
before = payment.__dict__.copy() # Otherwise save() (or valid?) will change the dict, needs to be here
oldcycle = payment.billingcycle
if request.method == 'POST':
form = Form(request.POST, instance=payment)
form.disable_fields()
if form.is_valid():
form.save()
newcycle = payment.billingcycle
if oldcycle != newcycle:
if oldcycle:
oldcycle.update_is_paid()
if newcycle:
newcycle.update_is_paid()
after = payment.__dict__
log_change(payment, request.user, before, after)
messages.success(request, unicode(_("Changes to payment %s saved.") % payment))
return redirect('payment_edit', id) # form stays as POST otherwise if someone refreshes
else:
messages.error(request, unicode(_("Changes to payment %s not saved.") % payment))
return redirect('payment_edit', id) # form clears otherwise, this is a borderline acceptable hack
else:
form = Form(instance=payment)
form.disable_fields()
logentries = bake_log_entries(payment.logs.all())
return render_to_response(template_name, {'form': form, 'payment': payment,
'logentries': logentries},
context_instance=RequestContext(request))