0

Have a simple function within a class . Function is as below :-

def addition(a,b):
      return a+b

Want the user to enter Two Numeric values within a Django Form , then add up these and show a result . Within the HTML Page have code as below :-

<form class="form" action="" method="post" name="NameForm" enctype=multipart/form-data>
          <div class="control-group">

          <div class="controls">
            <label for="user_input_1">Enter Numeric Input - 1: </label>
          {% csrf_token %}
           <input id="user_input_1" name="user_input_1"  value = "{{NameForm.user_input_1}}" type="text" placeholder="Enter Numeric Input - 1:" class="form-control input-xlarge search-query center-display"  required="True">
           </div>

 <div class="controls">
 <label for="user_input_1">Enter Numeric Input - 2: </label>
 <input id="user_input_2" name="user_input_2"  value = "{{NameForm.user_input_1}}" type="text" placeholder="Enter Numeric Input - 2:" class="form-control input-xlarge search-query center-display"  required="True">
 </div><!-- controls -->
 </div><!-- control-group -->

 <div class="control-group form-actions">
 <div class="controls">
 <button class="btn btn-primary btn-sm" value="Gogogo!" >Submit</button>
 </div><!-- controls -->
 </div><!-- control-group -->
 </form>

within my - views.py

sum_1 = obj.addition(numeric_1,numeric_2)# Simple Addition Function 

 ...

▼ Local vars Variable Value form

numeric_1 444443.0

numeric_2 555553.0

obj

request

# As i input Two Numeric values (floats) , numeric_1 = 444443.0 and numeric_2 = 555553.0 , the Django ERROR i get is => "addition() takes 2 positional arguments but 3 were given".

The Submit action is being considered as a "positional argument" to my simple "addition" function.

How can i avoid this - as seen from my - views.py , im summoning the function "addition" and passing the - numeric_1 = 444443.0 and numeric_2 = 555553.0 , arguments to it , im not wanting to pass the FORM SUBMIT ACTION to the "addition" function.

My views.py as seen below :-

def calc_a(request):

obj = calc_1() ## 
if request.method == 'POST':
# create a form instance and populate it with data from the request:

    form = NameForm(request.POST) ### NameForm is a Form in app_1/forms.py 
    #print("DHANK______Form Submitted with NUMBER")## ok...
    #result_1 = str(form["user_input_1"].value()) ## keep this INPUT as STR - Not FLOAT 
    #result_2 = str(form["user_input_2"].value()) ## keep this INPUT as STR - Not FLOAT 
    print("   "*30)
    #print("_NUMBER 1__SUBMITTED ___________",result_1) # terminal prints for stepping through code
    print("   "*30)
    #print("_NUMBER 2__SUBMITTED ___________",result_2)
    print("   "*30)
    #print("DHANK______Form Submitted with NUMBER_",form["user_input_1"].value()) ## ok...
    # check whether it's valid:
    if form.is_valid():
        #print("DHANK______Form Submitted is Valid_______")  ### DHANK -- prints - if Form is Valid 
        numeric_1 = float(form["user_input_1"].value())
        numeric_2 = float(form["user_input_2"].value())

        print("DHANK___numeric_2_______",numeric_2)
        print("DHANK___numeric_1________",numeric_1)

        sum_1 = obj.addition(numeric_1,numeric_2)  ### call the "addition" Function 
        print("sum_1____===",sum_1)

return render(request, 'app_1/calc_a.html') # 
Rohit Dhankar
  • 1,574
  • 18
  • 25

1 Answers1

0

Documenting my very simple slippage - The "addition" function needs the DEFAULT positional - parameter "self" , thus solved as seen below by adding "self" to the "addition" function ..wasnt really a Django forms issue.

def addition(self,a,b):
    return a+b
Rohit Dhankar
  • 1,574
  • 18
  • 25