網(wǎng)站首頁 編程語言 正文
前言
最近剛剛開始學(xué)習(xí)如何將python后臺(tái)與html前端結(jié)合起來,現(xiàn)在寫一篇blog記錄一下,我采用的是前后端不分離形式。
話不多說,先來實(shí)現(xiàn)一個(gè)簡單的計(jì)算功能吧,前端輸入計(jì)算的數(shù)據(jù),后端計(jì)算結(jié)果,返回結(jié)果至前端進(jìn)行顯示。
1.python開發(fā)工具
我選用的是pycharm專業(yè)版,因?yàn)樯鐓^(qū)版本無法創(chuàng)建django程序
2.項(xiàng)目創(chuàng)建
第一步:打開pycharm,創(chuàng)建一個(gè)django程序
藍(lán)圈圈起來的為自定義的名字,點(diǎn)擊右下角的create可以創(chuàng)建一個(gè)django項(xiàng)目
如下圖,圈起來的名字與上圖相對(duì)應(yīng)
第二步:編寫后端代碼
①在blog文件夾下面的views.py中編寫以下代碼:
from django.shortcuts import render
from calculate import jisuan
# Create your views here.
def calculate(request):
return render(request, 'hello.html')
def show(request):
x = request.POST.get('x')
y = request.POST.get('y')
result = jisuan(x, y)
return render(request, 'result.html', {'result': result})
②在csdn文件夾下面的urls.py中添加下面加粗部分那兩行代碼
from django.shortcuts import render
from calculate import jisuan
# Create your views here.
def calculate(request):
return render(request, 'hello.html')
def show(request):
x = request.POST.get('x')
y = request.POST.get('y')
result = jisuan(x, y)
return render(request, 'result.html', {'result': result})
③新建calculate.py文件,內(nèi)容為:
def jisuan(x, y):
x = int(x)
y = int(y)
return (x+y)
第三步:編寫前端代碼
①數(shù)據(jù)輸入的頁面hello.html,內(nèi)容為:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/getdata/">
{% csrf_token %}
<input type="text" name="x" placeholder="請(qǐng)輸入x"/><br>
<input type="text" name="y" placeholder="請(qǐng)輸入y"><br>
<input type="submit" value="進(jìn)行計(jì)算">
</form>
</body>
</html>
②結(jié)果返回的頁面result.html,內(nèi)容為:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 style="color:blue">計(jì)算結(jié)果為{{ result }}</h1>
</body>
</html>
第四步:啟動(dòng)后臺(tái)程序
在瀏覽器地址欄輸入http://127.0.0.1:8000/jisuan
回車可進(jìn)入數(shù)據(jù)輸入頁面
我們輸入x=10, y=20
點(diǎn)擊進(jìn)行計(jì)算按鈕,頁面跳轉(zhuǎn),顯示計(jì)算結(jié)果
?
?好啦,一個(gè)簡單的django項(xiàng)目就完成啦
如果想要進(jìn)行復(fù)雜的計(jì)算操作,可以在calculate.py編寫更加復(fù)雜的函數(shù)
源碼資源鏈接:django學(xué)習(xí),前后端不分離
總結(jié)
原文鏈接:https://blog.csdn.net/thzhaopan/article/details/123513504
相關(guān)推薦
- 2022-06-07 ?分享一個(gè)Python?遇到數(shù)據(jù)庫超好用的模塊_python
- 2022-06-10 python?PIL?Image?圖像處理基本操作實(shí)例_python
- 2022-05-29 一起來看看C語言的預(yù)處理注意點(diǎn)_C 語言
- 2022-08-26 Python?Matplotlib?marker?標(biāo)記詳解_python
- 2023-01-27 React?useEffect的理解與使用_React
- 2023-01-17 pytorch?geometric的GNN、GCN的節(jié)點(diǎn)分類方式_python
- 2022-06-30 Redis三種常用的緩存讀寫策略步驟詳解_Redis
- 2022-06-25 JQuery獲取對(duì)象的方式介紹_jquery
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支