我在我的一个Django项目网页上显示了一个表单 - 表单中的第一个字段接受文本输入字段中的整数,第二个字段在文本输入字段中显示另一个整数,第三个字段显示日期值。
目前,当用户首次加载此页面时,已填充第一和第二字段中显示的整数(第一个已从数据库中检索,第二个的值是根据第一个的值计算的) 。日期字段为空白。
但是,如果我更改第一个整数字段的值并刷新页面,则会自动加载字段1和字段2的新值(即用于显示此网页的view
将更改注册到value,并更新数据库,以便在重新加载页面时检索值时,新值就是它找到的值。
但是,出于某种原因,当我在第三个字段中选择日期并刷新页面时,重新加载页面时日期字段始终为空白...似乎日期未保存时输入值,或者在页面加载时不加载。
用于在浏览器中显示此页面的视图是:
def concept(request, project_id):
...
deposit = Deposit.objects.get_or_create(project=project)[0]
...
context = {
...
'deposit_form': DepositInfoForm(instance=deposit),
...
}
return render(request, 'projects/concept.html', context)
DepositInfoForm
上定义了我拥有卢布的'日期'字段forms.py
:
class DepositInfoForm(ValidatedForm):
amount_exc_vat = forms.IntegerField(required=False, widget=forms.NumberInput(attrs={'class': 'currency',}),label="Deposit exc VAT")
amount_inc_vat = forms.IntegerField(required=False, widget=forms.NumberInput(attrs={'class': 'currency', 'readonly':'readonly',}),label="Inc VAT")
date_received = mDateField(required=False, label="Date deposit received", widget=forms.DateInput(format='%d/%m/%Y', attrs=({'class':'datepicker'})))
class Meta:
model = Deposit
fields = ('amount_exc_vat', 'amount_inc_vat', 'date_received')#,'received')
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', {})
project = instance.project
try: amount_exc_vat = int(round(instance.amount_exc_vat))
except TypeError: amount_exc_vat = None
try: amount_inc_vat = int(round(project.deposit.amount_inc_vat))
except TypeError: amount_inc_vat = None
# ERF(21/11/2016 @ 11:00) Add a 'try' for date_deposit_received
#try: date_deposit_received = project.date_deposit_received
#except TypeError: date_deposit_received = None
initial = kwargs.get('initial', {})
initial={
'received': project.deposit_received,
'amount_exc_vat': amount_exc_vat,
'amount_inc_vat': amount_inc_vat,
}
kwargs['initial'] = initial
super(DepositInfoForm, self).__init__(*args, **kwargs)
self.fields['date_received'].widget.attrs.update({'data-original-value': self.initial['date_received'] or ''})
def save(self, commit=True):
deposit = self.instance
data = self.cleaned_data
if 'date_received' in self.changed_data:
if not data['date_received'] == '':
deposit.project.deposit_received = True;
self.deposit_r = True
deposit.project.upgrade_detailed_status(Project.ds75)
else:
deposit.project.deposit_received = False;
deposit.project.save()
if ('amount_exc_vat' in self.changed_data or 'date_received' in self.changed_data or 'received' in self.changed_data) and data['amount_exc_vat'] and data['date_received'] and data['received']:
send_message(message_template=2, project=self.instance.project)
self.deposit_r = True
if hasattr(self, 'deposit_r'):
# Make the payment
deposit_payment = Payment(project=deposit.project, is_booking_deposit=True, amount_exc_vat=data['amount_exc_vat'], date_paid=data['date_received'])
return super(DepositInfoForm, self).save(commit=commit)
我尝试将deposit_received
添加到def __init__(...)
方法,但得到了
AttributeError的
'Project'对象没有属性'deposit_received'
我认为加载页面时未显示日期的问题与__init__(...)
功能有关,但似乎无法解决问题是什么..任何人都有任何想法我怎么样可以使用保存的日期自动填充此字段吗?
修改
我通过更改已添加到的AttributeError
来解决try
:
try: date_received = project.deposit.date_received
except TypeError: date_received = None
但是,虽然页面现在正确加载并再次显示,但重新加载后,“日期”字段仍为空,而不是由用户在重新加载页面之前输入的值填充...
所以,似乎上面代码中的save
方法对date_received
字段无法正常工作 - 即使在该块中调用了save()
:
if 'date_received' in self.changed_data:
if not data['date_received'] == '':
deposit.project.deposit_received = True;
self.deposit_r = True
deposit.project.upgrade_detailed_status(Project.ds75)
else:
deposit.project.deposit_received = False;
deposit.project.save()