1.效果图:
2.制作步骤:
2.1.创建一个Django文件
若没有下载Django,可以利用终端下载一下
pip3 install Django -i https://pypi.tuna.tsinghua.edu.cn/simple
若已下载好,可直接利用终端在目标文件夹中创建Django,这里我命名为Calculator_System
django-admin startproject Calculator_System
在根目录Calculator_System下面,利用终端,我们再创建一个app,这里我命名为Calculator
python manage.py startapp Calculator
并在setting中注册app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Calculator.apps.CalculatorConfig', # 修改处
]
并在根目录Calculator_System下创建templates字典,用于存放HTML页面文件
在setting中不要忘了加入templates路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # 修改处
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
到此,Django的创建就完成了
2.2.路由urls的设置(若非零基础,建议从此处看):
urls.py文件:
from django.contrib import admin
from django.urls import path
from Calculator import views
urlpatterns = [
# path('admin/', admin.site.urls),
path('', views.main),
]
2.3.HTML文件的编写
在templates文件夹中,我们开一个HTML文件,这里我命名为main.html(这里我的代码有点繁琐,大佬们可以自己缩减一下)
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr style="height:70px;width:142">
{% for A in Answer_list %}
<td align="left"><h3>{{ A }}</h3></td>
{% endfor %}
</tr>
<tr>
<td>
<form method="post">
{% csrf_token %}
<table cellpadding="0px">
<tr>
<td><button style="height:50px;width:70px" type="submit" name="num_7"><h1>7</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_8"><h1>8</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_9"><h1>9</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="symbol_1"><h1>+</h1></button></td>
</tr>
<tr>
<td><button style="height:50px;width:70px" type="submit" name="num_4"><h1>4</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_5"><h1>5</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_6"><h1>6</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="symbol_2"><h1>-</h1></button></td>
</tr>
<tr>
<td><button style="height:50px;width:70px" type="submit" name="num_1"><h1>1</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_2"><h1>2</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="num_3"><h1>3</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="symbol_3"><h1>×</h1></button></td>
</tr>
<tr>
<td colspan="2"><button style="height:50px;width:142px" type="submit" name="num_0"><h1>0</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="symbol_4"><h1>.</h1></button></td>
<td><button style="height:50px;width:70px" type="submit" name="symbol_5"><h1>÷</h1></button></td>
</tr>
<tr>
<td colspan="2"><button style="height:50px;width:142px" type="submit" name="symbol_6"><h1>C</h1></button></td>
<td colspan="2"><button style="height:50px;width:142px" type="submit" name="symbol_7"><h1>=</h1></button></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
2.4.views文件的编写
在我们创建的app中,找到views.py文件,在其中编写计算器的基本程序(这里我写的也很繁琐,大佬们可以用函数缩减一下)
from django.shortcuts import render
# Create your views here.
list = ['', '', '']
def main(request):
method = request.method
if method == "GET":
return render(request, "main.html")
else:
num = ''
symbol = ''
global list
if list[1] == '':
if "num_0" in request.POST:
num = '0'
elif "num_1" in request.POST:
num = '1'
elif "num_2" in request.POST:
num = '2'
elif "num_3" in request.POST:
num = '3'
elif "num_4" in request.POST:
num = '4'
elif "num_5" in request.POST:
num = '5'
elif "num_6" in request.POST:
num = '6'
elif "num_7" in request.POST:
num = '7'
elif "num_8" in request.POST:
num = '8'
elif "num_9" in request.POST:
num = '9'
elif 'symbol_4' in request.POST:
num += '.'
list[0] += num
if list[0] != '' and list[2] == '':
if 'symbol_1' in request.POST:
symbol += '+'
elif 'symbol_2' in request.POST:
symbol += '-'
elif 'symbol_3' in request.POST:
symbol += '×'
elif 'symbol_5' in request.POST:
symbol += '÷'
list[1] += symbol
if len(list[1]) == 2:
list[1] = list[1][1:]
if list[1] != '':
if "num_0" in request.POST:
num = '0'
elif "num_1" in request.POST:
num = '1'
elif "num_2" in request.POST:
num = '2'
elif "num_3" in request.POST:
num = '3'
elif "num_4" in request.POST:
num = '4'
elif "num_5" in request.POST:
num = '5'
elif "num_6" in request.POST:
num = '6'
elif "num_7" in request.POST:
num = '7'
elif "num_8" in request.POST:
num = '8'
elif "num_9" in request.POST:
num = '9'
elif 'symbol_4' in request.POST:
num += '.'
list[2] += num
if "" not in list and 'symbol_7' in request.POST:
answer = Calculate(list)
# print(float(list[0]))
# print(float(list[2]))
print(answer)
answer_list = [list[0] + list[1] + list[2] + '=' + str(answer)]
list = ['', '', '']
return render(request, "main.html", {"Answer_list": answer_list})
if 'symbol_6' in request.POST:
list = ['', '', '']
print(list)
answer_list = [list[0] + list[1] + list[2]]
return render(request, "main.html", {"Answer_list": answer_list})
def Calculate(list):
if list[1] == '+':
answer = float(list[0]) + float(list[2])
elif list[1] == '-':
answer = float(list[0]) - float(list[2])
elif list[1] == '×':
answer = float(list[0]) * float(list[2])
elif list[1] == '÷':
answer = float(list[0]) / float(list[2])
return answer
至此,建议的计算器就制作好了,我们打开终端运行一下
python manage.py runserver
点击生成的网页,计算器就出来了
3.注意事项
该计算器在计算小数之间的算法时,可能会出现误差,这是由于浮点数二进制与十进制之间的转化导致的,各位大佬可以自己修改一下
文章可能会有错误或不足,欢迎大家指正,谢谢各位啦