最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年06月03日 10:16:17   作者:Venessa唯  
這篇文章主要介紹了Django微信小程序后臺開發(fā)教程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

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

2 添加交互框和按鈕

index. wxml

<!--index.wxml-->
<view class="container">
 <input type="text" class="input" 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
//獲取應(yīng)用實(shí)例
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 在服務(wù)器配置hello django

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

pip3 install django

創(chuàng)建django項(xiàng)目

django-admin startproject calculator
cd calculator

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

運(yùn)行hello django項(xiàng)目

cd calculator
python3 manage.py runserver 0.0.0.0:8000

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

4 實(shí)現(xiàn)計(jì)算器接口

創(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 admin
from django.urls import path
from django.conf.urls import url, include

urlpatterns = [
  path('admin/', admin.site.urls),
  url('^', include('CalculateApi.urls')),
]

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

from django.conf.urls import url
from . import views

urlpatterns = [
  url('calculate', views.calculate)
]

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

from django.http import HttpResponse


def calculate(request):
  formula = request.GET['formula']
  try:
    result = eval(formula, {})
  except:
    result = 'Error formula'
  return HttpResponse(result)

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

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

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

5.1 uwsgi配置

安裝uwsgi

pip3 install uwsgi

配置django項(xiàng)目的uwsgi.ini,在calculator文件夾中新建uwsgi.ini文件

touch uwsgi.ini
vi uwsgi.ini

輸入以下配置

[uwsgi]
# django項(xiàng)目監(jiān)聽的socket文件(可以使用端口代替)
socket = ./calculator.sock
# django項(xiàng)目所在目錄
chdir = .
# django項(xiàng)目wsgi文件
wsgi-file = ./calculator/wsgi.py

master = true
processes = 2
threads = 4
vacuum = true

# 通過touch reload可以重啟uwsgi服務(wù)器
touch-reload = ./reload
# 日志輸出
daemonize = calculator.log

運(yùn)行uwsgi服務(wù)器

uwsgi --ini uwsgi.ini
touch reload

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

安裝nginx

sudo apt-get install nginx
cd /etc/nginx

修改nginx用戶

vi nginx.conf

將第一行修改為

user root;

添加80端口的配置文件

cd conf.d
sudo touch calculator.conf
sudo vi calculator.conf

填入以下配置:

server{
  listen     80;
  server_name  服務(wù)器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服務(wù)器

sudo service nginx restart

訪問服務(wù)器的80端口即可訪問calculate接口,如http://服務(wù)器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服務(wù)器,訪問服務(wù)器的443端口即可訪問calculate接口,如https://服務(wù)器域名/calculate?formula=2*3-4

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

5.4 配置微信小程序的服務(wù)器信息

運(yùn)行小程序,一個簡單的計(jì)算器就寫完啦。

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

相關(guān)文章

  • python做反被爬保護(hù)的方法

    python做反被爬保護(hù)的方法

    在本文里小編給大家整理了一篇關(guān)于python做反被爬保護(hù)的方法的方法,由此需求的同學(xué)參考學(xué)習(xí)下。
    2019-07-07
  • python配置虛擬環(huán)境步驟

    python配置虛擬環(huán)境步驟

    大家好,本篇文章主要講的是python配置虛擬環(huán)境步驟,感興趣的同學(xué)趕快來看一看,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python使用turtle庫與random庫繪制雪花

    python使用turtle庫與random庫繪制雪花

    這篇文章主要為大家詳細(xì)介紹了python使用turtle庫與random庫繪制雪花,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • python繪制多個子圖的實(shí)例

    python繪制多個子圖的實(shí)例

    今天小編就為大家分享一篇python繪制多個子圖的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python ElementTree 基本讀操作示例

    python ElementTree 基本讀操作示例

    python ElementTree 基本讀操作示例
    2009-04-04
  • 終止python代碼運(yùn)行的3種方式詳析

    終止python代碼運(yùn)行的3種方式詳析

    這篇文章主要給大家介紹了關(guān)于終止python代碼運(yùn)行的3種方式,python是解釋運(yùn)行的程序,程序進(jìn)入死循環(huán)或者其它異常都會導(dǎo)致程序無法正常結(jié)束,需要的朋友可以參考下
    2023-07-07
  • Python2手動安裝更新pip過程實(shí)例解析

    Python2手動安裝更新pip過程實(shí)例解析

    這篇文章主要介紹了Python2手動安裝更新pip過程實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 如何使用Python代碼創(chuàng)建表格

    如何使用Python代碼創(chuàng)建表格

    如果能夠?qū)⑽覀兊臒o序數(shù)據(jù)快速組織成更易讀的格式,對于數(shù)據(jù)分析非常有幫助,下面這篇文章主要介紹了關(guān)于如何使用Python代碼創(chuàng)建表格的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Python中實(shí)現(xiàn)參數(shù)類型檢查的簡單方法

    Python中實(shí)現(xiàn)參數(shù)類型檢查的簡單方法

    這篇文章主要介紹了Python中實(shí)現(xiàn)參數(shù)類型檢查的簡單方法,本文講解使用裝飾器實(shí)現(xiàn)參數(shù)類型檢查并給出代碼實(shí)例,需要的朋友可以參考下
    2015-04-04
  • python中提高pip install速度

    python中提高pip install速度

    本文給大家分享了如何提高pip install速度的方法,其實(shí)就是將默認(rèn)源替換為國內(nèi)高速的源,非常的簡單實(shí)用,有需要的小伙伴可以參考下
    2020-02-02

最新評論

外汇| 方山县| 禹州市| 灵台县| 灌云县| 民乐县| 青冈县| 门源| 漾濞| 广德县| 竹溪县| 收藏| 沙湾县| 邮箱| 潮州市| 广宁县| 神农架林区| 舞钢市| 陵川县| 沈丘县| 临清市| 梨树县| 邯郸县| 大足县| 新建县| 南漳县| 洛川县| 青田县| 富顺县| 乐山市| 平潭县| 当雄县| 涿鹿县| 饶河县| 凤庆县| 武鸣县| 东光县| 巴林右旗| 肇庆市| 安远县| 鄂托克旗|