表格永远不会有效

时间:2017-03-15 17:42:02

标签: python django forms

我正在尝试创建一个网站

models.py

from django.db import models
from django.forms import ModelForm
class Calculator(models.Model):
    parents = models.CharField(max_length=100, blank=False)
    jobs = models.CharField(max_length=100, blank=False)
    grants_bursaries_scholarships = models.CharField(max_length=100, blank=False)
    student_loan = models.CharField(max_length=100, blank=False)
    other_income = models.CharField(max_length=100, blank=False)
    rent = models.CharField(max_length=100, blank=False)
    travel = models.CharField(max_length=100, blank=False)
    bills = models.CharField(max_length=100, blank=False)
    other_outcome = models.CharField(max_length=100, blank=False)
    def total_income(parents, jobs, grants_bursaries_scholarships, student_loan, other_income):
    return self.parents + self.jobs + self.grants_bursaries_scholarships + self.student_loan + self.other_income
    def fixed_outcome(rent, travel, bills, other_outcome):
        return self.rent + self.travel + self.bills + self.other_outcome
     def variable_outcomes(total_income, fixed_outcome):
         return self.total_income - self.fixed_outcome
     def food(variable_outcomes):
         return (43.66 * variable_outcomes) / 100.0
     def socialising(percent, whole):
         return (22.54 * variable_outcomes) / 100.0

forms.py

 from django import forms
 class MyCalculator(forms.Form):
     parents1 = forms.CharField(max_length=40, required=True)
      jobs1 = forms.CharField(max_length=50)
      grants_bursaries_scholarships1 = forms.CharField(max_length=40, required=True)
      student_loan1 = forms.CharField(max_length=40, required=True)
      other_income1 = forms.CharField(max_length=40, required=True)
      rent1 = forms.CharField(max_length=40, required=True)
      travel1 = forms.CharField(max_length=40, required=True)
      bills1 = forms.CharField(max_length=40, required=True)
      other_outcome1 = forms.CharField(max_length=40, required=True)

views.py

 from django import forms
 from .forms import MyCalculator
 def formview(request):
      if request.method == 'POST':
           form = MyCalculator(request.POST)
           if form.is_valid():
                myform = MyCalculator()
                myform.parents = form.cleaned_data.get(parents1)
                myform.jobs = form.cleaned_data.get(jobs1)
                myform.grants_bursaries_scholarships = form.cleaned_data.get(grants_bursaries_scholarships1)
                myform.student_loan = form.cleaned_data.get(student_loan1)
                myform.other_income = form.cleaned_data.get(other_income1)
                myform.rent = form.cleaned_data.get(rent1)
                myform.travel = form.cleaned_data.get(travel1)
                myform.bills = form.cleaned_data.get(bills1)
                myform.other_outcome = form.cleaned_data.get(other_outcome1)
                myform.total_income(myform.parents, myform.jobs, myform.grants_bursaries_scholarships, myform.student_loan, myform.other_income)
                myform.fixed_outcome(myform.rent, myform.travel, myform.bills, myform.other_outcome)
                myform.variable_outcomes(myform.total_income, myform.fixed_outcome)
                myform.save()
                return render(request, 'Noteable_Budgeting/out.html')
           else:
                return HttpResponseRedirect('/calculator/fail')
      else:
          form = MyCalculator()
      return render(request, 'Noteable_Budgeting/calculator_list.html', {'form':form})

 def failview(request):
      return render(request, 'Noteable_Budgeting/fail.html')

这是网站的主要视图

{% block titleblock %}
 <title>Noteable Budgeting</title>
{% endblock %}
{% block content %}
 <div class="text2">
  <h1>Budgeting Calculator</h1>
  <h2>Noteable Budgeting</h2>
  <br>
  <h3>
  Input your weekly incomes and fixed outcomes and our calculator will give you a recommended spending amount on food and social based on the average spent by students.
  </h3>
  <br>
  <form action="/calculator/test/" method="post">{% csrf_token %}
   <p>
   <div class="ui-widget">
    <label for="parents">Parents:</label>
    <input id="parents" type="number" name="parents" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="jobs">Jobs:</label>
    <input id="jobs" type="number" name="jobs" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="grants_bursaries_scholarship">Grants/Bursaries/Scholarships:</label>
    <input id="grants_bursaries_scholarship" type="number" name="grants_bursaries_scholarships" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="student_loan">Student Loan:</label>
    <input id="student_loan" type="number" name="student_loan" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="other_income">Other Income:</label>
    <input id="other_income" type="number" name="other_income" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="rent">Rent:</label>
    <input id="rent" type="number" name="rent" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="travel">Travel:</label>
    <input id="travel" type="number" name="travel" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="bills">Bills:</label>
    <input id="bills" type="number" name="bills" maxlength="100" />
    </div>
    </p>
    <p>
    <div class="ui-widget">
    <label for="other_outcome">Other Outcome:</label>
    <input id="other_outcome" type="number" name="other_outcome" maxlength="100" />
    </div>
    </p>
    <input type="submit" value="Submit" />
</form>
<br>
<br>
</div>
{% endblock %}

每当我提交表格时,如果是数字或空白,则会返回&#34; failview&#34;页。我希望它在提交表单时返回out.html页面并返回一些在models.py中计算的数字。

0 个答案:

没有答案