激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Django+Bootstrap實現(xiàn)計算器的示例代碼

Django+Bootstrap實現(xiàn)計算器的示例代碼

2022-02-25 13:26小旺不正經(jīng) Python

本文主要介紹了Django+Bootstrap實現(xiàn)計算器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

準(zhǔn)備工作

創(chuàng)建一個應(yīng)用:

Django+Bootstrap實現(xiàn)計算器的示例代碼

添加應(yīng)用到配置

Django+Bootstrap實現(xiàn)計算器的示例代碼

創(chuàng)建一個html

Django+Bootstrap實現(xiàn)計算器的示例代碼

編寫視圖函數(shù)

from django.shortcuts import render


# Create your views here.

def home(request):
  return render(request, 'index.html')

Django+Bootstrap實現(xiàn)計算器的示例代碼

配置路由

from django.contrib import admin
from django.urls import path,include

from app.views import home

urlpatterns = [
  path('admin/', admin.site.urls),
  path('',home,name='hoome'),
]

Django+Bootstrap實現(xiàn)計算器的示例代碼

 

導(dǎo)入Bootstrap前端框架

下載地址

將css、fonts、js復(fù)制到static文件夾下 沒有則創(chuàng)建該文件夾

Django+Bootstrap實現(xiàn)計算器的示例代碼

 

編寫前端內(nèi)容

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>計算器</title>
  <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" rel="external nofollow" >
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
  <script src="{% static 'js/bootstrap.min.js' %}"></script>

  <style>
      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;
      }
  </style>
</head>
<body>
<div class="container-fluid">
  <div class="row">
      <div class="col-xs-1 col-sm-4"> </div>
      <div id="computer" class="col-xs-1 col-sm-6">
          <input type="text" id="txt_code" name="txt_code" value="" class="form-control input_show" placeholder="" disabled>
          <input type="text" id="txt_result" name="txt_result" value="" class="form-control input_show" placeholder="結(jié)果" disabled>
          <br>
          <div>
              <button type="button" class="btn btn-default btn_num" onclick="fun_7()">7</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_8()">8</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_9()">9</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_div()">/</button>
              <br>
              <button type="button" class="btn btn-default btn_num" onclick="fun_4()">4</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_5()">5</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_6()">6</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_mul()">*</button>
              <br>
              <button type="button" class="btn btn-default btn_num" onclick="fun_1()">1</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_2()">2</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_3()">3</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_sub()">-</button>
              <br>
              <button type="button" class="btn btn-default btn_num" onclick="fun_0()">0</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_00()">00</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_dot()">.</button>
              <button type="button" class="btn btn-default btn_num" onclick="fun_add()">+</button>
          </div>
      <div>
      <br>
      <button type="button" class="btn btn-success btn-lg btn_clear" id="lgbut_clear" onclick="fun_clear()">
  清空
</button>
<button type="button" class="btn btn-primary btn-lg" id="lgbut_compute" >
  計算
</button>
  </div>
      </div>
          <div class="col-xs-1 col-sm-2"></div>
</div>
  </div>
<div class="extenContent"></div>
<script>
  var x=document.getElementById("txt_code");
  var y=document.getElementById("txt_result");
  function fun_7() {
      x.value+='7';
  }
  function fun_8() {
      x.value+='8';
  }
  function fun_9() {
      x.value+='9';
  }
  function fun_div() {
      x.value+='/';
  }
  function fun_4() {
      x.value+='4';
  }
  function fun_5() {
      x.value+='5';
  }
  function fun_6() {
      x.value+='6';
  }
  function fun_mul() {
      x.value+='*';
  }
  function fun_1() {
      x.value+='1';
  }
  function fun_2() {
      x.value+='2';
  }
  function fun_3() {
      x.value+='3';
  }
  function fun_sub() {
      x.value+='-';
  }
  function fun_0() {
      x.value+='0';
  }
  function fun_00() {
      x.value+='00';
  }
  function fun_dot() {
      x.value+='.';
  }
  function fun_add() {
      x.value+='+';
  }
  function fun_clear() {
      x.value='';
      y.value='';

  }
</script>
<script>
  function ShowResult(data) {
      var y = document.getElementById('txt_result');
      y.value = data['result'];
  }
</script>
<script>
  $('#lgbut_compute').click(function () {
      $.ajax({
          url:'compute/',
          type:'POST',
          data:{
              'code':$('#txt_code').val()
          },
          dataType:'json',
          success:ShowResult
      })
  })
</script>
</body>
</html>

 

編寫視圖函數(shù)

import subprocess

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST


def home(request):
  return render(request, 'index.html')


@csrf_exempt
def 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={'result': result})

Django+Bootstrap實現(xiàn)計算器的示例代碼

測試

Django+Bootstrap實現(xiàn)計算器的示例代碼

Django+Bootstrap實現(xiàn)計算器的示例代碼

Django+Bootstrap實現(xiàn)計算器的示例代碼

到此這篇關(guān)于Django+Bootstrap實現(xiàn)計算器的示例代碼的文章就介紹到這了,更多相關(guān)Django+Bootstrap計算器內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/weixin_42403632/article/details/121198578

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费国产在线视频 | 亚洲资源在线播放 | 久久国产精品99国产 | 99爱视频在线 | 亚洲99| 色视频欧美 | 一级做a爱片毛片免费 | 九九热在线免费观看视频 | 日本综合久久 | 一区二区免费网站 | 亚洲一区二区不卡视频 | 国产pron| 麻豆传传媒久久久爱 | 夜班护士在线观看 | 成人三级电影网址 | 欧美三级美国一级 | 欧美一级高清免费 | 欧美日韩在线播放一区 | 视频一区二区在线观看 | 久久9久久 | 欧美成人精品一区二区三区 | 色av综合在线 | 国产免费视频在线 | 一级美女大片 | 国产影院在线观看 | 黄色免费在线视频网站 | 成人啪啪色婷婷久 | 黄色毛片a级 | 天天看成人免费毛片视频 | 久久久久久中文字幕 | 好吊色37pao在线观看 | 免费欧美精品 | 久久婷婷一区二区三区 | 黄色片视频在线观看 | 国产女厕一区二区三区在线视 | 亚洲精品久久久久久久久久久 | 在线播放免费播放av片 | 国内精品免费一区二区2001 | 99re久久最新地址获取 | 亚洲精品com| 黄色影院一级片 |