Django寰俊灏忕▼搴忓悗鍙板紑鍙戞暀绋
鏈枃閾炬帴锛歨ttps://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绔彛锛変笅鐨刵ginx閰嶇疆
5.3 https鍗忚锛443绔彛锛変笅鐨刵ginx閰嶇疆
5.4 閰嶇疆寰俊灏忕▼搴忕殑鏈嶅姟鍣ㄤ俊鎭
1 鐢宠灏忕▼搴忥紝鍒涘缓hello world灏忕▼搴

鍦ㄥ井淇″紑鍙戝钩鍙帮紙https://mp.weixin.qq.com锛夌敵璇峰皬绋嬪簭骞惰幏鍙朅PP id


涓嬭浇寰俊寮鍙戣呭伐鍏凤紙https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html锛夛紝鎵撳紑鍚庣櫥褰曞苟濉叆APP id 绛変俊鎭
















2 娣诲姞浜や簰妗嗗拰鎸夐挳
index. wxml



    {{ result }}



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

index.js
//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
    })
  }
})



3 鍦ㄦ湇鍔″櫒閰嶇疆hello django
鍦ㄦ湇鍔″櫒瀹夎python3鍜宲ip3鐜锛屽苟瀹夎django
pip3 install django
鍒涘缓django椤圭洰

django-admin startproject calculator
cd calculator
淇敼calculator/settings.py涓殑ALLOWED_HOSTS = []涓篈LLOWED_HOSTS = ['*']

杩愯hello django椤圭洰

cd calculator
python3 manage.py runserver 0.0.0.0:8000


璁块棶http://鏈嶅姟鍣╥p:8000鍙互鐪嬪埌涓嬪浘锛








  

4 瀹炵幇璁$畻鍣ㄦ帴鍙
鍒涘缓django app
python3 manage.py startapp CalculateApi
鍦╟alculator/settings.py鐨処NSTALLED_APPS涓坊鍔燙alculateApi濡備笅锛

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CalculateApi'
]
鍦╟alculator/urls.py涓皢url杞彂缁機alculateApi澶勭悊銆

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')),
]
  

鍦–alculateApi涓柊寤簎rls.py鏂囦欢锛屽鐞/calculate鎺ュ彛銆

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

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

鍦–alculateApi/views.py鏂囦欢涓坊鍔燾alculate鍑芥暟鐢ㄤ簬璁$畻姹傚煎苟杩斿洖銆

from django.http import HttpResponse


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

鍐嶆杩愯鏈嶅姟鍣紝璁块棶http://鏈嶅姟鍣╥p:8000/calculate?formula=2*3-5鍗冲彲寰楀埌缁撴灉1銆








5 閰嶇疆鏈嶅姟鍣ㄥ皢鍚庣涓庡井淇″皬绋嬪簭杩炴帴
鐢变簬寰俊瑕佹眰浣跨敤https鍗忚杩涜閫氳锛屾垜浠娇鐢╪ginx + uwsgi + django鏉ラ厤缃甴ttps鏈嶅姟鍣ㄣ

5.1 uwsgi閰嶇疆
瀹夎uwsgi

pip3 install uwsgi
閰嶇疆django椤圭洰鐨剈wsgi.ini锛屽湪calculator鏂囦欢澶逛腑鏂板缓uwsgi.ini鏂囦欢

touch uwsgi.ini
vi uwsgi.ini
杈撳叆浠ヤ笅閰嶇疆

[uwsgi]
# django椤圭洰鐩戝惉鐨剆ocket鏂囦欢锛堝彲浠ヤ娇鐢ㄧ鍙d唬鏇匡級
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鏈嶅姟鍣

uwsgi --ini uwsgi.ini
touch reload
  

5.2 http鍗忚锛80绔彛锛変笅鐨刵ginx閰嶇疆
瀹夎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    鏈嶅姟鍣╥p;
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://鏈嶅姟鍣╥p/calculate?formula=2*3-4
5.3 https鍗忚锛443绔彛锛変笅鐨刵ginx閰嶇疆
濡傛灉鏈夎嚜宸辩殑鍩熷悕鍜宻sl璇佷功锛屽皢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

濡傛灉浣犲彧鏈夎嚜宸辩殑鍩熷悕鑰屾病鏈塻sl璇佷功锛屽彲浠ュ幓鐢宠鍏嶈垂鐨剆sl璇佷功鎴栬呭弬鑰冩缃戝潃閰嶇疆锛坔ttps://certbot.eff.org/#ubuntuxenial-nginx锛夈
濡傛灉浣犳病鏈夎嚜宸辩殑鍩熷悕鐢氳嚦娌℃湁鑷繁鐨勬湇鍔″櫒锛岃鍑洪棬鍙宠浆闃块噷浜戞垨宸﹁浆鑵捐浜戣嚜琛岃喘涔般

5.4 閰嶇疆寰俊灏忕▼搴忕殑鏈嶅姟鍣ㄤ俊鎭




杩愯灏忕▼搴忥紝涓涓畝鍗曠殑璁$畻鍣ㄥ氨鍐欏畬鍟︺





浠ヤ笂灏辨槸Django寰俊灏忕▼搴忓悗鍙板紑鍙戞暀绋嬬殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=390