Django+Bootstrap實(shí)現(xiàn)計(jì)算器的示例代碼
準(zhǔn)備工作
創(chuàng)建一個(gè)應(yīng)用

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

創(chuàng)建一個(gè)html

編寫(xiě)視圖函數(shù)
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'index.html')

配置路由
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'),
]

導(dǎo)入Bootstrap前端框架
將css、fonts、js復(fù)制到static文件夾下 沒(méi)有則創(chuàng)建該文件夾

編寫(xiě)前端內(nèi)容
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>計(jì)算器</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" rel="external nofollow" >
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/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" >
計(jì)算
</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>
編寫(xiě)視圖函數(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='輸入錯(cuò)誤'
return JsonResponse(data={'result': result})

測(cè)試



到此這篇關(guān)于Django+Bootstrap實(shí)現(xiàn)計(jì)算器的示例代碼的文章就介紹到這了,更多相關(guān)Django+Bootstrap計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 基于 tkinter 做個(gè)學(xué)生版的計(jì)算器
- Python雙版本計(jì)算器詳解
- python實(shí)現(xiàn)計(jì)算器簡(jiǎn)易版
- python 實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(gui界面)
- python 實(shí)現(xiàn)一個(gè)圖形界面的匯率計(jì)算器
- 用python實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器(完整DEMO)
- Python tkinter實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器代碼實(shí)例
- python如何通過(guò)閉包實(shí)現(xiàn)計(jì)算器的功能
相關(guān)文章
python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的全過(guò)程
最近項(xiàng)目中采用了前后端分離的技術(shù),感覺(jué)有必要給大家總結(jié)下,所以下面這篇文章主要給大家介紹了關(guān)于python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的相關(guān)資料,需要的朋友可以參考下2022-06-06
pyinstaller生成的exe文件啟動(dòng)時(shí)間漫長(zhǎng)的原因
本文主要介紹了pyinstaller生成的exe文件啟動(dòng)時(shí)間漫長(zhǎng)的原因,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Python利用subplots_adjust方法解決圖表與畫(huà)布的間距問(wèn)題
這篇文章主要介紹了如何在使用python?的?matplotlib庫(kù)繪圖時(shí),?使用subplots_adjust()方法來(lái)調(diào)整圖表與畫(huà)布之間的間距,以及圖表與圖表之間的間距,感興趣的可以了解一下2022-04-04
Python爬取阿拉丁統(tǒng)計(jì)信息過(guò)程圖解
這篇文章主要介紹了Python爬取阿拉丁統(tǒng)計(jì)信息過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python中使用OpenCV庫(kù)來(lái)進(jìn)行簡(jiǎn)單的氣象學(xué)遙感影像計(jì)算
這篇文章主要介紹了Python中使用OpenCV庫(kù)來(lái)進(jìn)行簡(jiǎn)單的氣象學(xué)圖像計(jì)算的例子,文中是用來(lái)進(jìn)行光譜輻射定標(biāo)、大氣校正和計(jì)算反射率,需要的朋友可以參考下2016-02-02
使用Python編寫(xiě)一個(gè)桌面便簽應(yīng)用
ChatGPT的編程能力也不差,本文將一步一步提出要求,讓ChatGPT根據(jù)我們的要求,編寫(xiě)出一個(gè)可用的,可打包運(yùn)行的桌面便簽,感興趣的可以了解一下2023-06-06
用pyqt5 給按鈕設(shè)置圖標(biāo)和css樣式的方法
今天小編就為大家分享一篇用pyqt5 給按鈕設(shè)置圖標(biāo)和css樣式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

