色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Django微信小程序后臺開發(fā)教程的實現(xiàn)

瀏覽:97日期:2022-07-05 18:58:02

1 申請小程序,創(chuàng)建hello world小程序

在微信開發(fā)平臺(https://mp.weixin.qq.com)申請小程序并獲取APP id

Django微信小程序后臺開發(fā)教程的實現(xiàn)

下載微信開發(fā)者工具(https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html),打開后登錄并填入APP id 等信息。

Django微信小程序后臺開發(fā)教程的實現(xiàn)

Django微信小程序后臺開發(fā)教程的實現(xiàn)

2 添加交互框和按鈕

index. wxml

<!--index.wxml--><view class='container'> <input type='text' bindinput=’input’/> <button bindtap='calculate'>cal</button> <view>{{ result }}</view></view>

index.wxss

/**index.wxss**/.input { border: 1px solid black; margin-bottom: 5px;}

index.js

//index.js//獲取應用實例const app = getApp()Page({ data: { result: '暫無結(jié)果', formula: ’’ }, //事件處理函數(shù) calculate: function () { wx.request({ url: ’https://shatter.xin/calculate’, data: { formula: this.data.formula }, success: res => { if (res.statusCode == 200) { this.setData({ result: res.data }) } } }) }, input: function (e) { this.setData({ formula: e.detail.value }) }})

3 在服務器配置hello django

在服務器安裝python3和pip3環(huán)境,并安裝django

pip3 install django

創(chuàng)建django項目

django-admin startproject calculatorcd calculator

修改calculator/settings.py中的ALLOWED_HOSTS = []為ALLOWED_HOSTS = [’*’]

運行hello django項目

cd calculatorpython3 manage.py runserver 0.0.0.0:8000

訪問http://服務器ip:8000可以看到下圖:

Django微信小程序后臺開發(fā)教程的實現(xiàn)

4 實現(xiàn)計算器接口

創(chuàng)建django app

python3 manage.py startapp CalculateApi

在calculator/settings.py的INSTALLED_APPS中添加CalculateApi如下:

INSTALLED_APPS = [ ’django.contrib.admin’, ’django.contrib.auth’, ’django.contrib.contenttypes’, ’django.contrib.sessions’, ’django.contrib.messages’, ’django.contrib.staticfiles’, ’CalculateApi’]

在calculator/urls.py中將url轉(zhuǎn)發(fā)給CalculateApi處理。

from django.contrib import adminfrom django.urls import pathfrom django.conf.urls import url, includeurlpatterns = [ path(’admin/’, admin.site.urls), url(’^’, include(’CalculateApi.urls’)),]

在CalculateApi中新建urls.py文件,處理/calculate接口。

from django.conf.urls import urlfrom . import viewsurlpatterns = [ url(’calculate’, views.calculate)]

在CalculateApi/views.py文件中添加calculate函數(shù)用于計算求值并返回。

from django.http import HttpResponsedef calculate(request): formula = request.GET[’formula’] try: result = eval(formula, {}) except: result = ’Error formula’ return HttpResponse(result)

再次運行服務器,訪問http://服務器ip:8000/calculate?formula=2*3-5即可得到結(jié)果1。

Django微信小程序后臺開發(fā)教程的實現(xiàn)

5 配置服務器將后端與微信小程序連接

由于微信要求使用https協(xié)議進行通訊,我們使用nginx + uwsgi + django來配置https服務器。

5.1 uwsgi配置

安裝uwsgi

pip3 install uwsgi

配置django項目的uwsgi.ini,在calculator文件夾中新建uwsgi.ini文件

touch uwsgi.inivi uwsgi.ini

輸入以下配置

[uwsgi]# django項目監(jiān)聽的socket文件(可以使用端口代替)socket = ./calculator.sock# django項目所在目錄chdir = .# django項目wsgi文件wsgi-file = ./calculator/wsgi.pymaster = trueprocesses = 2threads = 4vacuum = true# 通過touch reload可以重啟uwsgi服務器touch-reload = ./reload# 日志輸出daemonize = calculator.log

運行uwsgi服務器

uwsgi --ini uwsgi.initouch reload

5.2 http協(xié)議(80端口)下的nginx配置

安裝nginx

sudo apt-get install nginxcd /etc/nginx

修改nginx用戶

vi nginx.conf

將第一行修改為

user root;

添加80端口的配置文件

cd conf.dsudo touch calculator.confsudo vi calculator.conf

填入以下配置:

server{ listen 80; server_name 服務器ip; charset UTF-8; client_max_body_size 75M; location ~ ^/calculate { // replace 'path' to the path of your project uwsgi_pass unix:///'path'/calculator/calculator.sock; include /etc/nginx/uwsgi_params; }}

重啟nginx服務器

sudo service nginx restart

訪問服務器的80端口即可訪問calculate接口,如http://服務器ip/calculate?formula=2*3-4

5.3 https協(xié)議(443端口)下的nginx配置

如果有自己的域名和ssl證書,將calculator.conf配置文件修改如下:

server{ listen 443; server_name your.domain; ssl on; ssl_certificate path/to/your/ssl.pem; ssl_certificate_key path/to/your/ssl.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; charset UTF-8; client_max_body_size 75M; location ~ ^/calculate { uwsgi_pass unix:///path/to/calculator/calculator.sock; include /etc/nginx/uwsgi_params; }}

重啟nginx服務器,訪問服務器的443端口即可訪問calculate接口,如https://服務器域名/calculate?formula=2*3-4

如果你只有自己的域名而沒有ssl證書,可以去申請免費的ssl證書或者參考此網(wǎng)址配置(https://certbot.eff.org/#ubuntuxenial-nginx)。 如果你沒有自己的域名甚至沒有自己的服務器,請出門右轉(zhuǎn)阿里云或左轉(zhuǎn)騰訊云自行購買。

5.4 配置微信小程序的服務器信息

Django微信小程序后臺開發(fā)教程的實現(xiàn)

運行小程序,一個簡單的計算器就寫完啦。

Django微信小程序后臺開發(fā)教程的實現(xiàn)

到此這篇關(guān)于Django微信小程序后臺開發(fā)教程的實現(xiàn)的文章就介紹到這了,更多相關(guān)Django小程序后臺開發(fā)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: 微信
相關(guān)文章:
主站蜘蛛池模板: 国产一级毛片视频 | 日韩一区二区三区视频在线观看 | 久草在线中文最新视频 | 在线毛片一区二区不卡视频 | 国产伦精品一区二区三区四区 | 亚洲国产一区二区三区四区五区 | 女网址www呦 | 美女福利视频午夜在线 | 中国老太卖淫播放毛片 | 美国毛片一级视频在线aa | 国产一区二区日韩欧美在线 | 久久久国产精品福利免费 | 久久免费视频1 | 亚洲国产日韩成人综合天堂 | 中文在线三级中文字幕 | 国产一区私人高清影院 | 国产高清在线精品免费 | 特级毛片aaa免费版 特级毛片a级毛免费播放 | 白白在线观看永久免费视频 | 亚洲欧美视频网站 | 久久久欧美综合久久久久 | 久青草视频在线 | 一级香蕉免费毛片 | 国内交换一区二区三区 | 欧美成人 综合网播九公社 欧美成人26uuu欧美毛片 | 极品美女写真菠萝蜜视频 | 一级毛片免费不卡在线视频 | 亚洲热视频 | 国产精品无码久久久久 | 日产一区二区三区四区 | 免费一级淫片aaa片毛片a级 | 三级黄色在线 | 亚洲欧美日韩另类精品一区二区三区 | 天堂色视频 | 免费看一级欧美激情毛片 | 伊人久久网站 | 亚洲国产精品不卡毛片a在线 | 亚洲欧美成人综合久久久 | 污到下面流水的视频 | 日韩精品欧美国产精品亚 | 色综合a怡红院怡红院首页 色综合精品久久久久久久 色综合九九 |