1 Apply for an applet to create a hello world applet
In the microsoft development platform () Apply for the applet and get the APP id
Download WeChat Developer Tools (/miniprogram/dev/devtools/), open it, log in and fill in the APP id and other information.
2 Adding Interaction Boxes and Buttons
index. wxml
<!----> <view class="container"> <input type="text" class="input" bindinput='input'/> <button bindtap="calculate">cal</button> <view>{{ result }}</view> </view>
/****/ .input { border: 1px solid black; margin-bottom: 5px; }
// // Get the application instance const app = getApp() Page({ data: { result: "No results at this time.", formula: '' }, //Event Handling Functions calculate: function () { ({ url: '/calculate', data: { formula: }, success: res => { if ( == 200) { ({ result: }) } } }) }, input: function (e) { ({ formula: }) } })
3 Configuring hello django on the server
Install python3 and pip3 environment on the server and install django
pip3 install django
Creating a django project
django-admin startproject calculator cd calculator
Modify theALLOWED_HOSTS = []
because ofALLOWED_HOSTS = ['*']
Run the hello django project
cd calculator python3 runserver 0.0.0.0:8000
interviewshttp://server (computer)ip:8000
It can be seen in the picture below:
4 Implementing the Calculator Interface
Creating a django app
python3 startapp CalculateApi
In the calculator/INSTALLED_APPS
AddCalculateApi
Below:
INSTALLED_APPS = [ '', '', '', '', '', '', 'CalculateApi' ]
Forward the url in calculator/ to CalculateApi for processing.
from import admin from import path from import url, include urlpatterns = [ path('admin/', ), url('^', include('')), ]
Create a new file in CalculateApi to process the/calculate
Interface.
from import url from . import views urlpatterns = [ url('calculate', ) ]
Add the calculate function to the CalculateApi/ file to calculate the value and return it.
from import HttpResponse def calculate(request): formula = ['formula'] try: result = eval(formula, {}) except: result = 'Error formula' return HttpResponse(result)
Run the server again and access thehttp://server (computer)ip:8000/calculate?formula=2*3-5
The result 1 can be obtained.
5 Configure the server to connect the backend to the WeChat applet
Since WeChat requires the use of https protocol for communication, we use nginx + uwsgi + django to configure the https server.
5.1 uwsgi configuration
Install uwsgi
pip3 install uwsgi
To configure the django project, create a new file in the calculator folder
touch vi
Enter the following configuration
[uwsgi] # django project listening socket file (ports can be used instead) socket = ./ # django project directory chdir = . # django project wsgi files wsgi-file = ./calculator/ master = true processes = 2 threads = 4 vacuum = true # You can restart the uwsgi server with a touch reload. touch-reload = ./reload # Log output daemonize =
Running the uwsgi server
uwsgi --ini touch reload
5.2 nginx configuration for http protocol (port 80)
Installing nginx
sudo apt-get install nginx cd /etc/nginx
Modify the nginx user
vi
Change the first line to read
user root;
Adding a configuration file for port 80
cd sudo touch sudo vi
Fill in the following configuration:
server{ listen 80; server_name server (computer)ip; charset UTF-8; client_max_body_size 75M; location ~ ^/calculate { // replace "path" to the path of your project uwsgi_pass unix:///"path"/calculator/; include /etc/nginx/uwsgi_params; } }
Restart the nginx server
sudo service nginx restart
The calculate interface can be accessed by accessing port 80 of the server, as followshttp://server (computer)ip/calculate?formula=2*3-4
5.3 nginx configuration under https protocol (port 443)
If you have your own domain name and ssl certificate, change the configuration file as follows:
server{ listen 443; server_name ; ssl on; ssl_certificate path/to/your/; ssl_certificate_key path/to/your/; 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/; include /etc/nginx/uwsgi_params; } }
Restart the nginx server and access the calculate interface by accessing port 443 of the server, e.g.https://Server Domain Name/calculate?formula=2*3-4
- If you only have your own domain name without an ssl certificate, you can go to apply for a free ssl certificate or refer to this URL to configure it (/#ubuntuxenial-nginx).
- If you don't have your own domain name or even your own server, go out and turn right to AliCloud or left to TencentCloud and buy it yourself.
5.4 Configuring server information for WeChat applets
Run the applet and a simple calculator is written.
To this article on the realization of the Django WeChat small program background development tutorials are introduced to this article, more related Django small program background development content, please search for my previous posts or continue to browse the following related articles I hope that you will support me more in the future!