时间:2022-07-27 11:16:53 | 栏目:Python代码 | 点击:次
在开发的时候,用户要求在认证的时候自动添加xadmin登录账户和分配组权限
from django.contrib.auth.models import Group,User from django.http import JsonResponse def test(req): name=req.POST['name'] account=req.POST['account'] password=req.POST['password'] an=Group.objects.filter(id=1).first() #二级管理组 是管理员在xadmin后台添加的权限组 user = User(username=account) user.set_password(password) user.is_superuser = False user.is_active = True user.first_name = name user.is_staff = True user.save() #先生成用户 user.groups.add(an) return JsonResponse({'ret':0,'msg':'success'})
补充知识:django 登陆访问控制LoginRequiredMixin
mixin_utils.py
# _*_ coding:utf-8 _*_ from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator class LoginRequiredMixin(object): @method_decorator(login_required(login_url='/login/')) def dispatch(self,request,*args,**kwargs): return super(LoginRequiredMixin,self).dispatch(request,*args,**kwargs)
写view的时候,直接继承LoginRequiredMixin,如果未登录,则跳转到登陆页面