Django+Bootstrap实现计算器的示例代码
目录
- 准备工作
- 导入Bootstrap前端框架
- 编写前端内容
- 编写视图函数
准备工作 创建一个应用

文章图片
添加应用到配置

文章图片
创建一个html

文章图片
编写视图函数
from django.shortcuts import render# Create your views here.def home(request):return render(request, 'index.html')

文章图片
配置路由
from django.contrib import adminfrom django.urls import path,includefrom app.views import homeurlpatterns = [path('admin/', admin.site.urls),path('',home,name='hoome'),]

文章图片
导入Bootstrap前端框架 下载地址
将css、fonts、js复制到static文件夹下 没有则创建该文件夹

文章图片
编写前端内容
{% load static %}计算器 - 锐客网 body{background-position: center 0; background-repeat: no-repeat; background-attachment: fixed; background-size: cover; -webkit-background-size: cover; -o-background-size: cover; -moz-background-size: cover; -ms-background-size:cover; }.input_show{margin-top: 35px; max-width: 280px; height: 35px; }.btn_num{margin:1px 1px 1px 1px; width: 60px; }.btn_clear{margin-left: 40px; margin-right: 20px; }.extenContent{height: 300px; }
编写视图函数
import subprocessfrom django.http import JsonResponsefrom django.shortcuts import render# Create your views here.from django.views.decorators.csrf import csrf_exemptfrom django.views.decorators.http import require_POSTdef home(request):return render(request, 'index.html')@csrf_exemptdef compute(request):code = request.POST.get('code')try:code = 'print(' + code + ')'result = subprocess.check_output(['python', '-c', code], universal_newlines=True, stderr=subprocess.STDOUT,timeout=30)except:result='输入错误'return JsonResponse(data=https://www.it610.com/article/{'result': result})

文章图片
测试

文章图片

文章图片

文章图片
【Django+Bootstrap实现计算器的示例代码】到此这篇关于Django+Bootstrap实现计算器的示例代码的文章就介绍到这了,更多相关Django+Bootstrap计算器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM
- pytorch|使用pytorch从头实现多层LSTM