django+微信小程序

一、后台用django,前端用原生的微信小程序做完整的前后端的微信小程序。
微信小程序的前端界面:
django+微信小程序_第1张图片
wxml代码:



  
  


wxss代码:

page{
  height: 100%;
}
.container{
   height: 100%;
   display:flex;
   flex-direction: column;
   padding: 0;
   box-sizing: border-box;
   background-color: #f2f2f2

}
.login-icon{
  flex: none;
}
.log-img
{
width: 750rpx;
}
.login-from{
  margin-top: 20px;
  flex:auto;
  height: 100%;
}
.inputView{
  background-color: rgba(223, 205, 205, 0.767);
  line-height: 44px;
  width: 300px;
  

}
.loginLab
{
  margin: 15px 15px 15px 10px;
  color: #545454;
  font-size: 14px;

}
.inputText{
   flex: block;
  float:right;
  text-align: left;
  margin-right: 22px;
  margin-top: 11px;
  color: #574545;
  font-size: 14px;
}
.namelmage,.keylmage{margin-left: 22px;
width: 14px;
height:14px;


}
.line{
  width: 100%;
  height: 1px;
  background-color: #cccccc;
  margin-top: 1px;
}
.login{
  width: 100%;
  height:auto;
  
  margin-top: 0px;
  margin-bottom: 0px;
  padding-bottom:0px; 

}
.loginbtn{
  background-color: #28d130; 
  width: 80%;
  margin-top: 35px;
}

.info{
  text-align: center;
}

js代码:
const app = getApp()

Page({
data:{ //此处定义本页面中的全局变量
result: ‘’,
username: ‘’,
passwd: ‘’
},
inputName: function(e){ // 用于获取输入的账号
this.setData({
username: e.detail.value //将获取到的账号赋值给username变量
})
},
inputPwd: function (e) { // 用于获取输入的密码
this.setData({
passwd: e.detail.value //将获取到的账号赋值给passwd变量
})
},

log: function(e){ //与服务器进行交互
wx.request({
url: ‘http://192.168.1.1:8000/login’, //获取服务器地址,此处为本地地址
header:{
“content-type”: “application/x-www-form-urlencoded” //使用POST方法要带上这个header
},
method: “POST”,
data: { //向服务器发送的信息
username: this.data.username,
passwd: this.data.passwd
},
success: res => {
if (res.statusCode == 200) {
this.setData({
result: res.data //服务器返回的结果

二、django部分
项目目录:
django+微信小程序_第2张图片
model.py

from django.db import models

# Create your models here.


class User(models.Model):
    # 用户账号、要唯一
    userAccount = models.CharField(max_length=20, unique=True)
    # 密码
    userPasswd = models.CharField(max_length=20)
    # 昵称
    userName = models.CharField(max_length=20)
    # 手机号
    userPhone = models.CharField(max_length=20)
    # 地址
    userAdderss = models.CharField(max_length=100)
    # 头像路径
    userImg = models.CharField(max_length=150)
    # 等级
    userRank = models.IntegerField()
    # touken 验证值,每次登陆后都会更新
    userToken = models.CharField(max_length=50)
    @classmethod
    def createuser(cls, account, passwd, name, phone, address, img, rank, token):
        u = cls(userAccount = account, userPasswd = passwd, userName = name, userPhone = phone, userAdderss = address,\
                userImg = img, userRank = rank, userToken = token)
        return u

view.py:`
from .models import User

from django.http import HttpResponse

def login(request):
if request.method == “POST”:
username = request.POST.get(‘username’)
passwd = request.POST.get(‘passwd’)
try:
user = User.objects.get(userAccount=username)
if user.userPasswd != passwd:
return HttpResponse(“用户名或密码错误”)

    except User.DoesNotExist as e:
        return HttpResponse("用户名不存在")
    # 登录成功
    print(username)
    print(passwd)
    return HttpResponse("登录成功!")
else:
    return HttpResponse("请求错误")

`
最后放上成品:
django+微信小程序_第3张图片

你可能感兴趣的:(django)