models.py
from django.db import models
from django.contrib.auth.models import User
from registeredmember.models import Registeredmember
# Create your models here.
class Carloan_form(models.Model):
cost_of_vehicle_Naira = models.DecimalField(max_digits=10, decimal_places=2)
loan_repayment_tenure_Months = models.DecimalField(max_digits=10, decimal_places=2)
interest_rate_Percentage = models.DecimalField(max_digits=10, decimal_places=2)
equity_contrib_rate_Percentage = models.DecimalField(max_digits=10, decimal_places=2)
depreciation_rate_Percentage = models.DecimalField(max_digits=10, decimal_places=2)
user = models.ForeignKey(User, null=True)
time = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.user)
forms.py
from django import forms
from django.forms import ModelForm
from models import Carloan_form
class Carloan_formForm(ModelForm):
cost_of_vehicle_Naira = forms.DecimalField(label=(u'Cost of vehicle (in Naira)'))
loan_repayment_tenure_Months = forms.DecimalField(label=(u'Loan tenure (in Months)'))
interest_rate_Percentage = forms.DecimalField(label=(u'Interest rate (in %)'))
equity_contrib_rate_Percentage = forms.DecimalField(label=(u'Equity contribution rate (in %)'))
depreciation_rate_Percentage = forms.DecimalField(label=(u'Depreciation rate (in %)'))
class Meta:
model = Carloan_form
exclude = ('user',)
views.py
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
from forms import Carloan_formForm
@login_required
def index(request):
form = Carloan_formForm()
if request.POST:
form = Carloan_formForm(request.POST)
if form.is_valid():
form.save()
#Collection and Assignment of User input
amount_of_vehicle = float(form.cleaned_data['cost_of_vehicle_Naira'])
tenure = float(form.cleaned_data['loan_repayment_tenure_Months'])
interest_rate = float(form.cleaned_data['interest_rate_Percentage'])
equity = float(form.cleaned_data['equity_contrib_rate_Percentage'])
depreciation_rate = float(form.cleaned_data['depreciation_rate_Percentage'])
carloan = form.save(commit=False)
carloan.user = request.user
carloan.save()
#Class Definition
class LoanCalc:
def __init__(self,amount_of_vehicle,tenure,interest_rate,equity,depreciation_rate):
self.amount_of_vehicle = amount_of_vehicle
self.tenure = tenure
self.interest_rate = interest_rate
self.equity = equity
self.depreciation_rate = depreciation_rate
def interest(self):
return((self.interest_rate/100) * self.amount_of_vehicle *(self.tenure/12))
def management_fee(self):
return 0.01 * (self.amount_of_vehicle + self.interest())
def processing_fee(self):
return 0.0025 *(self.amount_of_vehicle + self.interest())
def legal_fee(self):
return 0.0075 *(self.amount_of_vehicle + self.interest())
def residual_amount(self):
return 0.01 * (self.amount_of_vehicle - ((self.depreciation_rate/100) * self.amount_of_vehicle *(self.tenure/12)))
def equity_contribution(self):
return (self.equity/100) * self.amount_of_vehicle
def total_amount(self):
return self.amount_of_vehicle+self.interest()+self.management_fee()+self.processing_fee()+self.legal_fee()+self.residual_amount()
def upfront_payment(self):
return self.management_fee() + self.processing_fee() + self.legal_fee() + self.equity_contribution() + self.residual_amount()
def opening_balance(self):
return self.total_amount() - self.upfront_payment()
def monthly_instalment(self):
return self.opening_balance()/self.tenure
def LoanPaymentPlan(self):
months = 1
total_amount = self.amount_of_vehicle+self.interest()+self.management_fee()+self.processing_fee()+self.legal_fee()+self.residual_amount()
upfront_payment = self.management_fee()+self.processing_fee()+self.legal_fee()+self.equity_contribution()+self.residual_amount()
opening_balance = total_amount - upfront_payment
balance = opening_balance
while months <= self.tenure:
if balance > 0:
monthly_instalment =(opening_balance/self.tenure)
monthly_interest = (((self.interest_rate/100) * balance)/ 12)
loan_payment = monthly_instalment - monthly_interest
closing_balance = balance - monthly_instalment
print ' ',months,' ',round(balance,2),' ', round(monthly_instalment,2),' ',round(monthly_interest,2) \
, ' ',' ',round(loan_payment,2),' ',round(closing_balance,2)
balance = closing_balance
months += 1
return 'Thank you for using the Loan Calc App'
#Creation of an instance with the name 'calc'
calc = LoanCalc(amount_of_vehicle,tenure,interest_rate,equity,depreciation_rate)
amountofVehicle = amount_of_vehicle
interest = calc.interest()
managementFee = calc.management_fee()
processingFee = calc.processing_fee()
legalFee = calc.legal_fee()
residualAmount = calc.residual_amount()
equityContribution = calc.equity_contribution()
totalAmount = calc.total_amount()
upfrontPayment = calc.upfront_payment()
openingBalance = calc.opening_balance()
loanpaymentplan =calc.LoanPaymentPlan()
#An empty form to be displayed alongside the result
forms = Carloan_formForm()
#Renders a template that displays the result
return render_to_response('carloan/result.html', {'form': forms, 'result':amountofVehicle , 'result1': interest, 'result2': managementFee, 'result3': processingFee,
'result4': legalFee, 'result5': residualAmount, 'result6': equityContribution, 'result7': totalAmount,
'result8': upfrontPayment, 'result9':openingBalance, 'result10': loanpaymentplan},
context_instance=RequestContext(request))
#If the user doesn't submit the form, it displays an empty form
else:
form = Carloan_formForm()
#Rendering a that template that displays an empty form
return render_to_response('carloan/index.html', {'form': form},
context_instance=RequestContext(request))
我的問題是;
- 有沒有一種方法可以保存傳遞給模板的結果?
- 有沒有一種方法可以發送我在Q1保存的結果。以上,並通過電子郵件發送給用戶(僅當用戶需要時)?
3.'result10' :loanpaymentplan`是我傳遞給模板,只打印一行,並在發展中不經過環和打印一切,但同時它在命令提示符下打印的一切(仍然)。什麼可能是錯的?如何從視圖響應保存到數據庫中的Django
1.請解決您的壓痕。 2.什麼是「當我呼籲['result10':loanpaymentplan]」是什麼意思?我不知道。 3.您的問題標題與您提出的四個實際問題中的任何一個有什麼關係? –
我編輯了它 – AJNinja
這裏最大的收穫是合併Carloan_form和LoanCalc,它解決了冗餘問題,並且需要保存結果,因爲每當您檢索記錄時都可以使用它們。對於按需發送電子郵件,您可以將'calc.pk'保存在'request.SESSION'中,或者將結果以表格的形式返回。使用'@ login_required'的另一個視圖可以將給定的pk與認證用戶相匹配。如果一切正常,您可以獲取記錄並通過電子郵件發送,如我的答案中所示。祝你好運! –