Simple Calculator using HTML forms in python django
Question: Below is my html form:

    
    
    <form id="calci_form" method="get" action="#">
     <input type="hidden" name="prev_val" value="{{prev_val}}"></input>
     <input type="hidden" name="curr_val" value="{{curr_val}}"></input>
     <input type="hidden" name="op_sign" value="{{opsign}}"></input>
     <div id="calculator">
     <table id="tableCalci">
     <tr  id="row1">
     <td colspan="4"><input type="text" value="0" class="display" name="user_input">{{result}}</input></td>
    </tr>
     <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     </tr>
    <tr class="hover">
    <td><button id="7" name="numval" value="7" type="submit"></button></td>
    <td><button id="8" name="numval" value="8" type="submit"></button></td>
    <td><button id="9" name="numval" value="9" type="submit"></button></td>
    <td><button id="plus" name="sym" type="submit" value="add">&plus;</button>      
    </td>
    </tr>
    <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
    </tr>
     <tr class="hover">
    <td><button id="4" name="numval" value="4" type="submit"></button></td>
    <td><button id="5" name="numval" value="5" type="submit"></button></td>
    <td><button id="6" name="numval" value="6" type="submit"></button></td>
    <td><button id="minus" name="sym" value="minus" type="submit">&minus;   
    </button></td>
    </tr>
    <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
      <td>&nbsp;</td>
     <td>&nbsp;</td>
    </tr>
     <tr class="hover">
     <td><button id="1" name="numval" value="1" type="submit"></button></td>
     <td><button id="2" name="numval" value="2" type="submit"></button></td>
     <td><button id="3" name="numval" value="3" type="submit"></button></td>
     <td><button id="times" name="sym" value="times" type="submit">&times;  
     </button></td>
     </tr>
     <tr>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
     </tr>
     <tr class="hover">
     <td>&nbsp;</td>
     <td><button id="0" name="numval" value="0" type="submit"></button></td>
     <td><button id="equal" name="sym" type="submit">&equals;</button></td>
     <td><button id="divide"; name="sym" type="submit" value="divide">&divide;   
     </button></td>
     </tr>
     </table>
     </div>
    </form>
    

Views.py file:

    
    
    if 'sym' in request.GET:
            if request.GET['sym'] == 'add':
                first=request.GET['result']
                opsign='\&plus;'
                return render(request,'calculator.html',{'result':first,'prev_val':first,'curr_val':second,'opsign':opsign}) 
    elif request.GET['sym'] == '=':
                if 'prev_val' in request.GET and request.GET['prev_val']:
                    first=request.GET['prev_val']
                if 'result' in request.GET and request.GET['result']:
                    second=request.GET['result']    
                try:
                    result=add(10,20)
                except ValueError:
                    err="Error: Incorrect Number"
                except ZeroDivisionError:
                    err="Error: Division by zero"
                return render(request,'calculator.html',{'result':result,'error':err}) 
            else:
                return render(request,'calculator.html',{'error':'No Operation selected'}) 
    

Solution required: When i click the "Plus" button or "Equal" button the above
mentioned function does not invoke at all. THe controller is not passed from
HTML page to the above function in the views.py file. Where am i making the
mistake and why the controller is not passing control to the function. Any
help is appreciated.


Answer: In my view.py file i had written the operations such as add,sub,mul in the
end. So, i just put them at the top of the file below import. It worked.

Views.py file:

    
    
    from django.shorcuts import render
    
    def add(a,b):
     return a+b
    
    def mul(a,b):
     return a*b
    
    def sub(a,b):
     return a-b
    
    def operation(request):
      if 'sym' in request.GET:
            if request.GET['sym'] == 'add':
                first=request.GET['result']
                opsign='\&plus;'
                return render(request,'calculator.html',{'result':first,'prev_val':first,'curr_val':second,'opsign':opsign}) 
      elif request.GET['sym'] == '=':
            if 'prev_val' in request.GET and request.GET['prev_val']:
                first=request.GET['prev_val']
            if 'result' in request.GET and request.GET['result']:
                second=request.GET['result']    
            try:
                result=add(10,20)
            except ValueError:
                err="Error: Incorrect Number"
            except ZeroDivisionError:
                err="Error: Division by zero"
            return render(request,'calculator.html',{'result':result,'error':err}) 
        else:
            return render(request,'calculator.html',{'error':'No Operation selected'}) 
    

