我无法从视图访问deposit()
类中的Account
方法。给我错误:deposit() missing 1 required positional argument: 'amount'
我用以下方法调用该方法:
Account.deposit(amount)
观看次数:
def home(request):
return render(request, 'automatedbankteller/home.html')
def about(request):
return render(request, 'automatedbankteller/about.html')
@login_required
def transactions(request):
context = {
'transactions': Transaction.objects.filter(sender=request.user)
}
return render(request, 'automatedbankteller/transactions.html', context)
@login_required
def my_accounts(request):
context = {
'accounts': Account.objects.filter(account_owner=request.user)
}
return render(request, 'automatedbankteller/my_accounts.html', context)
@login_required
def deposit(request):
if request.method == 'POST':
form = UserDepositForm(request.POST)
if form.is_valid():
amount = request.POST['amount']
print(amount)
Account.deposit(amount)
#form.save()
messages.success(request, f'The amount has been deposited into your account!')
return redirect('ABT-my_accounts')
else:
form = UserDepositForm(instance=request.user)
return render(request, 'automatedbankteller/deposit.html', {'form': form})
型号:
class Account(models.Model):
ACCOUNT_TYPES = (
('SA', 'Savings'),
('CA', 'Checking'),
)
accountID = models.IntegerField()
account_amount = models.DecimalField(max_digits=20, decimal_places=2)
account_type = models.CharField(max_length=2, choices=ACCOUNT_TYPES)
account_owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return str(self.accountID)
def get_balance(self):
return self.account_amount
def deposit(self, amount):
self.account_amount = self.account_amount + amount
return
def withdraw(self, amount):
self.account_amount = self.account_amount - amount
return
网址:
urlpatterns = [
path('', views.home, name='ABT-home'),
path('about/', views.about, name='ABT-about'),
path('my_accounts/', views.my_accounts, name='ABT-my_accounts'),
path('account_history/', views.transactions, name='ABT-transactions'),
path('deposit/', views.deposit, name='ABT-deposit'),
]
表格:
class UserDepositForm(forms.ModelForm):
amount = forms.DecimalField(max_digits=20, decimal_places=2)
class Meta:
model = Account
fields = ['account_type', 'amount']
答案 0 :(得分:1)
Account.deposit(amount)这里的帐户是class。您需要在此类的实例上调用deposit方法。
account = Account.objects.get(id=1)
account.deposit(amount)
答案 1 :(得分:0)
您的Account
需要两个参数-类实例和数量。因此,您需要首先创建一个save()
实例,然后使用该方法。另外,由于您不返回更新的金额,因此可以在方法本身内部调用class Account(models.Model):
...
def deposit(self, amount):
self.account_amount = self.account_amount + amount
self.save()
return
方法。
deposit
您需要先在实例上调用# views.py
@login_required
def deposit(request):
if request.method == 'POST':
form = UserDepositForm(request.POST)
if form.is_valid():
amount = request.POST['amount']
print(amount)
account = form.save(commit=False) #no commit in DB
account.deposit(amount) # use class instance
messages.success(request, f'The amount has been deposited into your account!')
return redirect('ABT-my_accounts')
else:
form = UserDepositForm(instance=request.user)
return render(request, 'automatedbankteller/deposit.html', {'form': form})
。
deposit
您还可以通过save()
方法返回更新后的金额,但是您将需要自己致电{{1}}。