在python django中使用HTML表单的简单计算器

时间:2022-05-26 20:27:49

Below is my html form:

以下是我的html表单:

<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:

Views.py文件:

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.

需要的解决方案:当我单击“加号”按钮或“等于”按钮时,上述功能根本不会被调用。控制器不会从HTML页面传递到views.py文件中的上述函数。我在哪里犯了错误,为什么控制器没有将控制权传递给函数。任何帮助表示赞赏。

2 个解决方案

#1


1  

is your view getting called or you're not able to get any result.

是你的观点被调用,或者你无法获得任何结果。

One thing even if view gets control, is sure that your code will break when sym is = because it will be handled in the first if and it doesn't handle =

即使视图获得控制,有一件事是确保当sym =时你的代码会中断,因为它将在第一个if处理而它不处理=

If your form isn't submitted, i think you need to handle it through Javascript and AJAX.

如果您的表单未提交,我认为您需要通过Javascript和AJAX处理它。

#2


0  

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.

在我的view.py文件中,我最后编写了add,sub,mul等操作。所以,我只是把它们放在导入下面的文件的顶部。有效。

Views.py file:

Views.py文件:

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'}) 

#1


1  

is your view getting called or you're not able to get any result.

是你的观点被调用,或者你无法获得任何结果。

One thing even if view gets control, is sure that your code will break when sym is = because it will be handled in the first if and it doesn't handle =

即使视图获得控制,有一件事是确保当sym =时你的代码会中断,因为它将在第一个if处理而它不处理=

If your form isn't submitted, i think you need to handle it through Javascript and AJAX.

如果您的表单未提交,我认为您需要通过Javascript和AJAX处理它。

#2


0  

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.

在我的view.py文件中,我最后编写了add,sub,mul等操作。所以,我只是把它们放在导入下面的文件的顶部。有效。

Views.py file:

Views.py文件:

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'})