1.通过django-admin startproject xxx 建立一个django工程
2.进入到和manage.py同级目录下,通过Python manage.py startapp zzz项目
3.在zzz文件下的urls中设置将要访问的路由
注意引用views文件 from views import *
url(r'^login/',login),
url(r'^register/',regist),
4.接口的编写
这是注册接口
def regist(req):
if req.method == 'POST':
uf = UserForm(req.POST)
print("hgoahoahoasd")
if uf.is_valid():
#获得表单数据
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
# createdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
#添加到数据库
yw_new_users.objects.create_user(username= username,password=password)=
return render(req,"login.html")
else:
uf = UserForm()
return render(req,"regist.html")
这是登录接口
def login(req):
if req.method == 'POST':
# uf = UserForm(req.POST)
username = req.POST.get("UserName")
password = req.POST.get("UserPswd")
user = auth.authenticate(username = username,password = password)
print(user)
print("7894565")
if user:=
logss(username,password)=
print(type(user))
# for key in user:
# user = req.POST.get(key)[0]
auth_login(req,user)
return HttpResponse(json.dumps({"status":"ok","response":username}),content_type = "application/json")
else:
#比较失败,还在login
return render(req,"login.html")
return render(req,"login.html")
5.models文件的创建
from __future__ import unicode_literals
from django.db import models
from datetime import datetime
from django.contrib.auth.models import AbstractUser
# Create your models here.
# 后台用户信息表
class yw_new_users(AbstractUser):
Userid = models.AutoField(u'用户原有id',primary_key=True)
# UserName = models.CharField(u'用户名字',max_length=20,default="")
UserTrueName = models.CharField(u'用户真实姓名',max_length=20,null=True)
UserRoles = models.CharField(u'用户角色',max_length=15,null=True)
UserEmail = models.CharField(u'用户邮箱',max_length=100,null=True)
UserPhone = models.CharField(u'用户电话',max_length=15,null=True)
User_Status = models.CharField(u'用户状态',max_length=15,null=True)
LastMarks = models.CharField(u'用户痕迹',max_length=100,null=True)
HashCode = models.CharField(u'散列表',max_length=50,null=True)
enable_flags = models.CharField(u'有效性',max_length=50,null=True)
role_code = models.CharField(u'用户角色',max_length=50,null=True)
createDate = models.DateField(u'创建日期',null=True)
updateDate = models.DateField(u'更新日期',null=True)
createUser = models.CharField(u'创建用户',max_length=50,null=True)
updateUser = models.CharField(u'更新用户',max_length=50,null=True)
注意在setting.py 文件中需要设置AUTH_USER_MODEL = "zzz.yw_new_users"
6.通过Python manage.py makemigrations生成迁移文件,然后 Python manage.py migrate生成数据库
7.login页面展示登录页面:
8.注册界面展示注册页面: