Django+Bootstrap实现计算器的示例代码

目录

  • 准备工作
  • 导入Bootstrap前端框架
  • 编写前端内容
  • 编写视图函数

准备工作 创建一个应用
Django+Bootstrap实现计算器的示例代码
文章图片

添加应用到配置
Django+Bootstrap实现计算器的示例代码
文章图片

创建一个html
Django+Bootstrap实现计算器的示例代码
文章图片

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

Django+Bootstrap实现计算器的示例代码
文章图片

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

Django+Bootstrap实现计算器的示例代码
文章图片


导入Bootstrap前端框架 下载地址
将css、fonts、js复制到static文件夹下 没有则创建该文件夹
Django+Bootstrap实现计算器的示例代码
文章图片


编写前端内容
{% 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实现计算器的示例代码
文章图片

Django+Bootstrap实现计算器的示例代码
文章图片

【Django+Bootstrap实现计算器的示例代码】到此这篇关于Django+Bootstrap实现计算器的示例代码的文章就介绍到这了,更多相关Django+Bootstrap计算器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读