【发布时间】:2019-12-24 02:38:53
【问题描述】:
我有一个小型 Django 计算器应用程序。我想在单击任何数字按钮时在文本框中显示值
我的 HTML 代码
<form action ="buttonClicked" method="POST">
{% csrf_token %}
<div class="calculator">
<input type="text" class="calculator-screen" value="0" disabled name = "text-box"/>
<div class="calculator-keys">
<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">-</button>
<button type="button" class="operator" value="*">×</button>
<button type="button" class="operator" value="/">÷</button>
<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>
<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>
<button type="submit" value="1" name="btn1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>
<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>
<button type="button" class="equal-sign" value="=">=</button>
</div>
</div>
</form>
{% endblock %}
我的视图.py
from django.shortcuts import render
from django.contrib import messages
def calc(request):
if request.method == 'POST':
#here i want to take the button value and display it on text box
return render(request, 'Calculator.html')
Calc
如果我按下按钮 1,那么它应该在文本框中显示 1... 我该怎么做?
解决了我的问题
查看.py
def calc(request):
if(request.POST.get('btn1')):
print('Button clicked')
return render(request, 'Calculator.html',{'val':100})
else:
return render(request, 'Calculator.html',{'val':0})
【问题讨论】:
-
使用J-query设置输入值
-
我只想用python来做。谢谢