1 # gbacc_urls.py 2 # ————————38PerfectCRM实现全局账号登录注销———————— 3 from django.conf.urls import url 4 from gbacc import gbacc_views 5 urlpatterns = [ 6 url(r‘^gbacc_login/‘, gbacc_views.gbacc_login, name=‘gbacc_login‘), # 全局登录 7 # LOGIN_URL = ‘/gbacc/gbacc_login/‘# login_url 配置,默认‘/accounts/login/‘ #注意 / (斜杠,绝对路径)#settings.py 8 9 url(r‘^gbacc_logout/‘, gbacc_views.gbacc_logout, name=‘gbacc_logout‘), # 全局注销,默认跳转到accounts/login 10 11 # ————————39PerfectCRM实现全局账号注册———————— 12 url(r‘^gbacc_register/‘, gbacc_views.gbacc_register, name=‘gbacc_register‘), # 注册 13 # ————————39PerfectCRM实现全局账号注册———————— 14 15 # ————————40PerfectCRM实现全局密码修改———————— 16 url(r‘^(\d+)/gbacc_modify/$‘,gbacc_views.gbacc_modify, name=‘gbacc_modify‘), 17 # 全局logout django logout 默认跳转到accounts/login 18 # ————————40PerfectCRM实现全局密码修改———————— 19 20 21 ] 22 # ————————38PerfectCRM实现全局账号登密码密码录注销————————
# gbacc_urls.py
1 # gbacc_views.py 2 # ————————38PerfectCRM实现全局账号登录注销———————— 3 from django.contrib.auth import login #记录登录 #Django在数据库创建一条记录 #记住密码,免登录 4 from django.contrib.auth import authenticate #调用用户认证模块 5 from django.contrib.auth import logout #注销功能 6 from django.shortcuts import render #页面返回 7 from django.shortcuts import redirect #页面返回 8 9 #全局账号登录 10 def gbacc_login(request): 11 errors={} 12 if request.method =="POST": 13 _email=request.POST.get(‘email‘) 14 _password=request.POST.get(‘password‘) 15 user =authenticate(username=_email,password=_password)#调用用户认证模块 16 print(‘认证账号密码‘,user) 17 if user: 18 login(request,user)#记录登录 #Django在数据库创建一条记录 #记住密码,免登录 19 next_url =request.GET.get(‘next‘,‘/‘)#跳转的页面,默认为首页 20 return redirect(next_url) 21 else: 22 errors[‘error‘]=‘认证失败!‘ 23 return render(request,‘gbacc_specific/gbacc_login.html‘,locals()) 24 25 #全局账号注销 26 def gbacc_logout(request): 27 logout(request) #调用Djangao 注销功能 28 return redirect(‘/gbacc/gbacc_login/‘) 29 30 # ————————38PerfectCRM实现全局账号登录注销———————— 31 32 33 34 35 36 # ————————39PerfectCRM实现全局账号注册———————— 37 38 #json 对错误信息对象进行处理 #处理注册的内容 39 import json #转为json格式 40 from django.core.exceptions import ValidationError #错误信息 41 class JsonCustomEncoder(json.JSONEncoder): 42 def default(self,field): 43 if isinstance(field,ValidationError):#如果是错误信息进行处理 44 return {‘code‘:field.code ,‘messages‘:field.messages} 45 else: 46 return json.JSONEncoder.default(self,field) 47 48 #注册 ajax 验证 49 from gbacc.gbacc_auxiliary.account import RegisterForm #注册验证 50 from django.contrib.auth.hashers import make_password #密码加密 51 from django.shortcuts import HttpResponse #页面返回 52 from crm import models 53 def gbacc_register(request): 54 if request.method==‘GET‘: 55 obj=RegisterForm(request=request, data=request.POST) #注册验证 #PerfectCRM.gbacc_auxiliary.account 56 return render(request, ‘gbacc_specific/gbacc_register.html‘,{‘obj‘:obj}) 57 elif request.method==‘POST‘: 58 #返回的字符串 字典 59 ret={‘status‘:False,‘error‘:None,‘data‘:None} 60 #进行验证 调用RegisterForm 61 obj=RegisterForm(request=request, data=request.POST)#注册验证 #PerfectCRM.gbacc_auxiliary.account 62 if obj.is_valid(): 63 name = obj.cleaned_data.get(‘name‘)#获取用户名 64 password = obj.cleaned_data.get(‘password‘) #获取密码 65 email= obj.cleaned_data.get(‘email‘) #获取邮箱账号 66 password=make_password(password,)#密码加密 67 #print(username,password,email) 68 #——————数据库添加数据—————— 69 models.UserProfile.objects.create(name=name,password=password,email=email,) 70 #——————数据库添加数据—————— 71 #获取用户数据 72 user_info= models.UserProfile.objects. 73 filter(name=name, password=password). 74 values(‘id‘, ‘name‘, ‘email‘,).first() 75 #nid=user_info.id 76 print(user_info,type(user_info),‘..........‘) 77 # admin_obj = base_admin.site.registered_sites[‘crm‘][‘userprofile‘]#表类 78 # user_obj=admin_obj.model.objects.get(id=user_info[‘id‘])#类表的对象 79 # user_obj.set_password(password)#加密 80 # user_obj.save() 81 request.session[‘user_info‘] = user_info # session 82 #print(user_info.id) 83 ret[‘status‘]=True #状态 84 ret[‘data‘]=obj.cleaned_data 85 # print(obj.cleaned_data) 86 # print(ret) 87 ret=json.dumps(ret)#转为json格式 88 #return HttpResponse(ret) 89 else: 90 #加入错误信息 91 #print(obj.errors) 92 ret[‘error‘]=obj.errors.as_data() 93 #提示为False 94 #ret[‘status‘]=False 95 #对错误信息对象进行转化处理 前端不用二次序列化 96 ret=json.dumps(ret,cls=JsonCustomEncoder) 97 #print(ret) 98 return HttpResponse(ret) 99 100 # ————————39PerfectCRM实现全局账号注册———————— 101 102 103 # ————————40PerfectCRM实现全局密码修改———————— 104 from king_admin import base_admin 105 from king_admin import forms as kingforms 106 def gbacc_modify(request,user_id):#用户密码修改 107 admin_obj = base_admin.site.registered_sites[‘crm‘][‘userprofile‘]#表类 108 model_form = kingforms.CreateModelForm(request,admin_obj=admin_obj)#modelform 生成表单 加验证 109 obj=admin_obj.model.objects.get(id=user_id)#类表的对象 110 errors={}#错误提示 111 if request.method==‘POST‘: 112 ret={} 113 _password0=request.POST.get(‘password0‘) 114 user =authenticate(username=obj.email,password=_password0)#调用用户认证模块 115 print(‘obj.email‘,obj.email) 116 print(‘验证比对数据库‘,user) 117 _password1=request.POST.get(‘password1‘) 118 _password2=request.POST.get(‘password2‘) 119 if user: 120 if _password1==_password2: 121 if len(_password1)>6: 122 obj.set_password(_password1)#继承Django方法 #加密 123 obj.save() 124 ret=‘密码修改成功!‘ 125 return redirect(‘/gbacc/gbacc_login/‘) 126 else: 127 errors[‘password_too_short‘]=‘必须不少于6字符‘ 128 else: 129 errors[‘invalid_password‘]=‘两次输入的密码不一样‘ 130 else: 131 errors[‘invalid_password‘] = ‘原密码错误‘ 132 return render(request,‘gbacc_specific/gbacc_modify.html‘,locals()) 133 # ————————40PerfectCRM实现全局密码修改————————
# gbacc_views.py
1 {#global_modify.html#} 2 {## ————————40PerfectCRM实现全局密码修改————————#} 3 {% extends "gbacc_master/gbacc_sample.html" %} 4 {% block right-container-content %} 5 <div class="container col-lg-offset-3"> 6 <form class="form-signin col-lg-4 pu " method="post" >{% csrf_token %} 7 <div><h2 class="form-signin-heading">CRM 修改密码</h2> </div> 8 <span style="color: red">{{ model_form.errors }}</span> 9 <div> 10 <label class="col-sm-4 control-label " style="font-weight: normal"> 用户名: </label> 11 <label><input type="text" name="user" class="form-control" value="{{ obj }}" disabled> </label> 12 </div> 13 <div> 14 <label class="col-sm-4 control-label " style="font-weight: normal"> 原密码:</label> 15 <label><input type="password" name="password0" class="form-control"> </label> 16 </div> 17 <div> 18 <label class="col-sm-4 control-label " style="font-weight: normal"> 新密码:</label> 19 <label><input type="password" name="password1" class="form-control"> </label> 20 </div> 21 <div> 22 <label class="col-sm-4 control-label " style="font-weight: normal"> 新密码(重复): </label> 23 <label><input type="password" name="password2" class="form-control"></label> 24 </div> 25 <div > 26 {% for k, v in errors.items %} 27 <span style="color: red">{{ v }}</span> 28 {% endfor %} 29 </div> 30 <div><input type="submit" value="提交" class="pull-right btn btn-info " ></div> 31 </form> 32 </div> 33 {% endblock %} 34 {## ————————40PerfectCRM实现全局密码修改————————#}
{#global_modify.html#}
原文地址:https://www.cnblogs.com/ujq3/p/8761830.html
时间: 2024-10-09 01:01:14