-1

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))

私の質問は;

  1. テンプレートに渡された結果を保存する方法はありますか?
  2. Q1で保存した結果を送信する方法はありますか?上記のように、電子メールでユーザーに送信しますか(ユーザーが希望する場合のみ)?

3.「result10」:テンプレートに渡すloanpaymentplan`は、1行だけを印刷し、ループを通過してすべてを印刷するのではなく、コマンドプロンプトですべてを印刷します(まだ開発中です)。何が間違っている可能性がありますか?

4

1 に答える 1

1

あなたはこのようなものを見ることができます:

モデル

from django.db import models
class LoanCalc(models.Model):
    amount_of_vehicle = models.DecimalField()
    tenure = models.DecimalField()
    interest_rate = models.DecimalField()
    equity = models.DecimalField()
    depreciation_rate = models.DecimalField()

    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())

景色

# views.py
from django.core.mail import send_mail
from django.template.loader import get_template
from django.template import Context

from .models import LoanCalc

def loan_calc(request, **kwargs):
    calc = LoanCalc.objects.create(**kwargs)

    # Render your calc model in a template and use send_mail to email it
    send_mail(
        'This is your loan calculation!',
        get_template('carloan/email.txt').render(
            Context({
                'calc': calc,
            })
        ),
        'you@example.com',
        ['receiver@gexample.com']
    )

    return render_to_response('carloan/result.html',
                              {'form': forms, 'calc': calc}
                              context_instance=RequestContext(request))

ビューテンプレート

# result.html
<table>
    <tr>
       <td>Interest</td>
       <td>{{ calc.interest }}</td>
    </tr>
    <tr>
       <td>Management fee</td>
       <td>{{ calc.management_fee }}</td>
    </tr>
</table>

メールテンプレート

# email.txt
Hi, this is your loan calculation overview:

Interest: {{ calc.interest }}
Management fee: {{ calc.management_fee }}
于 2012-11-14T09:13:20.647 に答える