我收到的格式错误表示字段不是日期格式字段。我不知道为什么它会给我一个与我想填写的表格字段无关的表格...
以下是显示的确切错误:
ValidationError at /transfer/
["'0' value has an invalid date format. It must be in YYYY-MM-DD format."]
Request Method: POST
Request URL: http://localhost:8000/transfer/
Django Version: 1.8.6
Exception Type: ValidationError
Exception Value:
["'0' value has an invalid date format. It must be in YYYY-MM-DD format."]
Exception Location: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py in to_python, line 1287
Python Executable: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\OmarJandali\\Desktop\\opentab\\opentab',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
以下是模型文件:
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) # server
first_name = models.CharField(max_length=25, default='first')
last_name = models.CharField(max_length=25, default='last')
dob = models.DateField(default='0')
city = models.CharField(max_length=45, default='city') # user
state = models.CharField(max_length=25, default='state')
phone = models.BigIntegerField(default=0) # user
privacy = models.SmallIntegerField(default=1) # user
balance = models.DecimalField(decimal_places=2, max_digits=9, default=0)
created = models.DateTimeField(auto_now_add=True) # server
这是form.py:
class TransferForm(forms.ModelForm):
acct_choices = (('Tabz', 'Tabz - Username'),
('Wells Fargo', 'Wells Fargo - Username'))
main = forms.TypedChoiceField(
choices=acct_choices
)
transfer = forms.TypedChoiceField(
choices=acct_choices
)
class Meta:
model = Transfers
fields = ['main', 'transfer', 'amount', 'memo']
这是处理表单并创建新配置文件的部分:
错误在第9-14行
if main == 'Tabz':
profiles = Profile.objects.all()
for profile in profiles:
if currentUser == profile.user:
currentProfile = profile
currentProfile.balance = currentProfile.balance - amount
currentProfile.save()
else:
new_balance = amount
new_profile = Profile.objects.create(
user = currentUser,
balance = new_balance,
)
message = 'You have transfered ' + amount + ' from your Tabz account to main account'
new_activity = Acitivty.objects.create(
user = currentUser,
description = message,
status = 1,
category = 1,
)
if transfer == 'Tabz':
profiles = Profile.objects.all()
for profile in profiles:
if currentUser == profile.user:
currentProfile = profile
currentProfile.balance = currentProfile.balance + amount
currentProfile.save()
else:
new_balance = amount
new_profile = Profile.objects.create(
user = currentUser,
balance = new_balance
)
message = 'You have transfered ' + amount + ' from your Tabz account to main account'
new_activity = Acitivty.objects.create(
user = currentUser,
description = message,
status = 1,
category = 1,
)
return redirect('home_page')
答案 0 :(得分:3)
因为您已经使用了DateField,并且您提供了一个defualt值为0,因此错误即将来临,
所以改变这一行
dob = models.DateField(default='0')
到
dob = models.DateField(default='1900-01-01')
然后makemigrations
和migrate
。然后尝试提交表单