calculated form field

139 views
Skip to first unread message

bolivar4

unread,
Mar 19, 2012, 11:41:27 AM3/19/12
to django...@googlegroups.com
Want to present users a form in which numbers are entered, then manipulated in a view and a calculated value is returned to the screen.
After looking in the the Django book around the net this is what I have come, (it doesn't work) shows below.  So far, the three fields setup in forms.py render to the secreen, that is all I'm getting.
 
I am getting this error in the django 404 exception page: "unsupported operand type(s) for *: 'NoneType' and 'NoneType'"
 
It is purposely a  simple exercise, more interested  in technique of doing this so, I know this can be done in javascript, but I want to do this with python/django.
Thank you to anyone who can help me out, this has been a problem all weekend.
 
forms.py:
 
from django import forms
 
class FinanceTable(forms.Form):
       capital = forms.IntegerField(required=False)
       tax_rate = forms.DecimalField(required=False)
       useage = forms.IntegerField(required=False)
 
views.py:
 
from django.shortcuts import render_to_response
 
def  calculation(request):
       """view that sets up form calculation"""
       if request.method == 'POST':
            form  = FinanceTable(request.POST)
            if form.is_valid():
                 capital = form.cleaned_data['capital']
                  tax_rate = form.cleaned_data['tax_rate']
                  useage = form.cleaned_data['capital'] * form.cleaned_data['tax_rate']                 
                  response_dict = {'capital' : capital, 'tax_rate'  : tax_rate, 'useage' : useage}
                  return render_to_response('focus_areas.html', response_dict)
        else:
              form = FinanceTable(
                             initial={'capital' : 1000, tax_rate : .07}
               )
        return render_to_response('focus_areas.html', {'form' : form}) 
 
 
html in template:
          <form action="" method= "" method="post">
                  <table>
                          {{ form.as_table }}
                  </table>
                  <input type="submit" value = "Submit"/>
          </form>
 
 
Any help ios much appreciated.
 
 
 
 
 
 
 
 
 
 
              

Joel Goldstick

unread,
Mar 19, 2012, 12:54:40 PM3/19/12
to django...@googlegroups.com

What does your urls.py file look like. If you are getting 404 then it
may not be finding your view.
--
Joel Goldstick

Tim Ney

unread,
Mar 19, 2012, 1:09:09 PM3/19/12
to django...@googlegroups.com
Thank you for responding, this is a vexing problem.
I know the urs.py is fine, as the the form html code I posted
is part of an .html, largely static html, which renders to the screen fine.
The form fields are also showing up in the rendered page, and numbers can be
entered, when the submit button is clicked I get the exception

Did I not understand your question, I am new at this.

 



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Rajeesh Nair

unread,
Mar 19, 2012, 1:10:17 PM3/19/12
to django...@googlegroups.com

Your FormClass expects all 3 fields to be optional (required=False). But your code in view always expects them to have integer values. And you end up multiplying values from two blank fields! Either you provide some default value to the fields or rewrite view to use form.cleaned_data.get with a default value as 2nd argument to it.


On Monday, March 19, 2012 9:11:27 PM UTC+5:30, bolivar4 wrote:
 
"unsupported operand type(s) for *: 'NoneType' and 'NoneType'"
 

Tim Ney

unread,
Mar 19, 2012, 2:51:35 PM3/19/12
to django...@googlegroups.com
Rajeesh,
 
Following your advice, Im think I've about solved the problem.
I've got one last exception, I hope.
 
Here is the new iteration of views.py, all other files remain the same.
In short the page renders to the screen, the problem arises when values are submitted
to the view.
 
I get an exception  "can't multiply sequence by non-int of type 'tuple". I've created a new
function that describes the multiplication operation, that is used within the render function, this
is where the error occcrs. I'm not referencing the variables properly, I guess.
 
def calculation(request):
     """function that computes submission"""
    if request.method == 'POST':
         form = FinanceTable(request.POST)
         if form.is_valid():
           cd =form.cleaned_data
           capital = cd['capital'],
           tax_rate = cd['tax_rate'],
           useage = projfin(request, capital, tax_rate)
           response_dict = {'capital' : capital, 'tax_rate' : tax_rate, 'useage' : useage}
           return render_to_response('textplusnumbers.html' , response_dict)
   else:
         form = FinanceTable(
                      initial={'capital' : 1000, 'tax_rate' : .07}
                      )
 
   return render_to_response('textplusnumbers.html' , {'form' : form})
 
def projfin(request, capital, tax_rate):
     useage = capital * tax_rate
     return useage          
 
 
 
 
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/CFfpFTLoPSMJ.

Babatunde Akinyanmi

unread,
Mar 20, 2012, 3:17:30 AM3/20/12
to django...@googlegroups.com
Why not just do:

useage = capital * tax_rate
Instead of your projfin function. In the projfin function, the request
you are passing is also redundant.

As for the error, I don't know how you refactored your models code but
obviously from the error message, you are not passing in ints. You can
try to debug the code by printing capital and tax_rate so you can see
what values are being passed into the function.

>> Your FormClass expects all 3 fields to be optional (*required=False*).


>> But your code in view always expects them to have integer values. And you
>> end up multiplying values from two blank fields! Either you provide some
>> default value to the fields or rewrite view to use

>> *form.cleaned_data.get*with a default value as 2nd argument to it.


>>
>>
>> On Monday, March 19, 2012 9:11:27 PM UTC+5:30, bolivar4 wrote:
>>
>>>
>>>
>>> "unsupported operand type(s) for *: 'NoneType' and 'NoneType'"
>>>
>>>
>>> class FinanceTable(forms.Form):

>>> capital = forms.IntegerField(required=**False)
>>> tax_rate = forms.DecimalField(required=**False)
>>> useage = forms.IntegerField(required=**False)


>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/django-users/-/CFfpFTLoPSMJ.
>>
>> To post to this group, send email to django...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Tim Ney

unread,
Mar 20, 2012, 7:57:13 AM3/20/12
to django...@googlegroups.com
Thank you for your response.
 
1. The fact that I'm using a function and then referencing the function in the
        render should not make a difference.
2. I'm not using a database model.
3. Where is the redundancy?
4. When you respond to a request for help an example
      of how you would solve the problem whould help, that I is why
      I took the time to type in th the forms.py file, the views.py and the html.
5. You don't need to respond to this.
Reply all
Reply to author
Forward
0 new messages