本文連接:https://blog.csdn.net/qq_43467898/article/details/83187698
Django微信小程序後臺開發教程
1 申請小程序,建立hello world小程序
2 添加交互框和按鈕
3 在服務器配置hello django
4 實現計算器接口
5 配置服務器將後端與微信小程序鏈接
5.1 uwsgi配置
5.2 http協議(80端口)下的nginx配置
5.3 https協議(443端口)下的nginx配置
5.4 配置微信小程序的服務器信息
1 申請小程序,建立hello world小程序html
<!--index.wxml--> <view class="container"> <input type="text" class="input" bindinput='input'/> <button bindtap="calculate">cal</button> <view>{{ result }}</view> </view>
/**index.wxss**/ .input { border: 1px solid black; margin-bottom: 5px; }
//index.js //獲取應用實例 const app = getApp() Page({ data: { result: "暫無結果", formula: '' }, //事件處理函數 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 }) } })
pip3 install django
django-admin startproject calculator cd calculator
修改calculator/settings.py中的ALLOWED_HOSTS = []
爲ALLOWED_HOSTS = ['*']
python
運行hello django項目nginx
cd calculator python3 manage.py runserver 0.0.0.0:8000
http://服務器ip:8000
能夠看到下圖:
python3 manage.py startapp CalculateApi
INSTALLED_APPS
中添加CalculateApi
以下:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'CalculateApi' ]
from django.contrib import admin from django.urls import path from django.conf.urls import url, include urlpatterns = [ path('admin/', admin.site.urls), url('^', include('CalculateApi.urls')), ]
/calculate
接口。from django.conf.urls import url from . import views urlpatterns = [ url('calculate', views.calculate) ]
from django.http import HttpResponse def 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
便可獲得結果1。
因爲微信要求使用https協議進行通信,咱們使用nginx + uwsgi + django來配置https服務器。django
pip3 install uwsgi
touch uwsgi.ini vi uwsgi.ini
輸入如下配置ubuntu
[uwsgi] # django項目監聽的socket文件(能夠使用端口代替) socket = ./calculator.sock # django項目所在目錄 chdir = . # django項目wsgi文件 wsgi-file = ./calculator/wsgi.py master = true processes = 2 threads = 4 vacuum = true # 經過touch reload能夠重啓uwsgi服務器 touch-reload = ./reload # 日誌輸出 daemonize = calculator.log
uwsgi --ini uwsgi.ini touch reload
sudo apt-get install nginx cd /etc/nginx
vi nginx.conf
將第一行修改成小程序
user root;
cd conf.d sudo touch calculator.conf sudo 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; } }
sudo service nginx restart
http://服務器ip/calculate?formula=2*3-4
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證書或者參考此網址配置(https://certbot.eff.org/#ubuntuxenial-nginx)。
若是你沒有本身的域名甚至沒有本身的服務器,請出門右轉阿里雲或左轉騰訊雲自行購買。服務器
運行小程序,一個簡單的計算器就寫完啦。微信
--