无法在django中乘以IntegerFields的值

时间:2018-02-02 20:22:58

标签: python django user-interface textfield

我对如何在django中乘以两个IntegerFields的值感到困惑。我对django比较新,所以我可能会遗漏一些相当明显的东西。我的代码给了我以下异常: " ValueError at / multiplication / multiplied 视图multiplication.views.post没有返回HttpResponse对象。它返回了None。 请求方法:POST" 这是我的views.py中的代码:

       from django.shortcuts import render,get_object_or_404
       from django.shortcuts import render_to_response
       from django.template import RequestContext
       from django.views.generic import TemplateView
       from django import forms
       from multiplication.forms import HomeForm

       template_name1 = 'multiplication/detail.html'
       template_name2 = 'multiplication/multiplied.html'

       class myForm(forms.Form):
       quantity1 = forms.IntegerField(required=False)
       quantity2 = forms.IntegerField(required=False)

       form = myForm()

       def get(request):
           return render(request,template_name1,{'form': form} )
       def multiply_two_integers(x,y):
           return x*y

      def post(request):
          if (form.is_valid()):
             x = request.POST('quantity1')
             y = request.POST('quantity2')
             product = multiply_two_integers(x, y)
             return render(request, template_name2, {'form': form, 
             'product': product })

模板1:

             <h1>Multiplication Function</h1>
             <form action =  "{% url 'multiplication:post' %}" method = 
             "post">
             {{ form.as_p  }}
             {% csrf_token %}
             <input type = "submit" value ="Multiply">
             <!--<button type="submit"> Multiply </button>-->
             <h1>{{product}}</h1>
             </form>

模板2:

             <h1>{{product}}</h1>

1 个答案:

答案 0 :(得分:0)

当您的表单有效时,后卫if (form.is_valid())无法通过,因此post函数会返回None而不是呈现应该HttpResponse

因此异常的性质。