首页 » Python » python3 高级篇 » 正文

Django框架–菜单登录设计demo

总入口:

from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url('admin/', admin.site.urls),
    url(r'^auth/', include('auth_server.urls')),
]

在settings.py中注册咱们的app

数据表:

from django.db import models

# Create your models here.

class Host(models.Model):
    hostname = models.CharField(max_length=64, blank=True, null=True, verbose_name='主机名')
    ecsname = models.CharField(max_length=64, blank=True, null=True, verbose_name='实例ID')
    logining = models.ManyToManyField(to='Login', blank=True, null=True, verbose_name='授权用户')
    login_port = models.CharField(max_length=16, default='22', blank=True, null=True, verbose_name='ssh登录端口')
    cpu = models.CharField(max_length=8, blank=True, null=True, verbose_name='CPU')
    mem = models.CharField(max_length=8, blank=True, null=True, verbose_name='内存/G')
    speed = models.CharField(max_length=8, blank=True, default='5', null=True, verbose_name='带宽/M')
    disks = models.ManyToManyField(to='Disk', blank=True, null=True, verbose_name='磁盘')
    network = models.CharField(max_length=32, blank=True, null=True, verbose_name='IP')
    sn = models.CharField(max_length=64, blank=True, null=True, verbose_name='sn')
    kernel = models.CharField(max_length=64, blank=True, null=True, verbose_name='系统内核')  # 内核+版本号
    source = models.ForeignKey(to='Source', blank=True, null=True, verbose_name='来源类型')
    region = models.ForeignKey(to='Region', blank=True, null=True, verbose_name='所属区域')
    state_choices = (
        (1, 'Running'),
        (2, '下线'),
        (3, '关机'),
        (4, '删除'),
        (5, '故障'),
    )
    state = models.SmallIntegerField(verbose_name='主机状态', choices=state_choices, blank=True, null=True, )

    def __str__(self):
        return self.network


    class Meta:
        verbose_name_plural = "主机表"


class Disk(models.Model):
    '''磁盘'''
    path = models.CharField(max_length=64, blank=True, null=True, verbose_name='挂载路径')
    size = models.CharField(max_length=16, blank=True, null=True, verbose_name='磁盘大小/G')
    remarks = models.CharField(max_length=2048, blank=True, null=True, verbose_name='备注')
    def __str__(self):
        return self.size
    class Meta:
        verbose_name_plural = "磁盘表"


class Region(models.Model):
    name = models.CharField(max_length=64,blank=True,null=True,verbose_name='区域')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = "区域表"


class Login(models.Model):
    '''登录相关'''
    login_name = models.CharField(max_length=16, default='root', verbose_name='登录用户名')
    login_pwd= models.CharField(max_length=64, blank=True, null=True, verbose_name='登录密码')
    auth=models.CharField(max_length=8,blank=True, null=True, verbose_name='具有权限')
    def __str__(self):
        return self.login_name
    class Meta:
        verbose_name_plural = "主机用户表"


class Source(models.Model):
    '''来源:阿里云、物理机(某机房等)'''
    name = models.CharField(max_length=16, blank=True, null=True, verbose_name='来源')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "主机来源表"


###################权限部分

class UserInfo(models.Model):       ##用户表
    name = models.CharField(max_length=16, blank=True, null=True, verbose_name="用户名")
    password = models.CharField(max_length=128, blank=True, null=True, verbose_name="密码")
    email = models.CharField(max_length=32, verbose_name="邮箱")
    userPosition = models.ForeignKey(to="Position", blank=True, null=True, verbose_name="职位", related_name="userposi")  # 用户和职位是一对多的关系

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "用户表"


class Position(models.Model):       ##职位表
    name = models.CharField(max_length=16, blank=True, null=True, verbose_name="职位名")
    auth = models.ManyToManyField(to='Auth', blank=True, null=True, verbose_name="权限", related_name="posauth")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "职位表"


class Auth(models.Model):           ##权限表
    name = models.CharField(max_length=64, blank=True, null=True, verbose_name="显示路径名")
    url = models.CharField(max_length=64, blank=True, null=True, verbose_name="路径")
    groupauth = models.ForeignKey(to='AuthGroup', blank=True, null=True, verbose_name="组权限", related_name="authgroup")      #组和权限是一对多的关系,一个组有多个权限


    def __str__(self):
        return self.url

    class Meta:
        verbose_name_plural = "权限表"


class AuthGroup(models.Model):      ##权限组表
    name = models.CharField(max_length=16, blank=True, null=True, verbose_name="权限组名")
    menu = models.ForeignKey(to='Menu', blank=True, null=True, verbose_name="菜单", related_name='groupmenu')     #一个组下有多个菜单

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "权限组表"


class Menu(models.Model):       ##菜单表
    name = models.CharField(max_length=16, blank=True, null=True, verbose_name="菜单名")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "菜单表"

mark

权限表:

mark

职位表:

开发:查看主机的权限;
- /auth/host/list.html

测试:查看所有权限 + 增加主机的权限;
- /auth/host/list.html
- /auth/host/add.html
- /auth/user/list.html
- /auth/authgroup/list.html
- /auth/menu/list.html

运维:除了不可以删除主机,有全部权限;

BOSS:全部权限;

views.py

from django.shortcuts import render,HttpResponse,redirect

# Create your views here.
from auth_server import models


def demo(request):
	if request.method == 'GET':

		return render(request, 'login.html', locals())
	else:
		username = request.POST.get('username')
		password = request.POST.get('password')
		user_obj_li = models.UserInfo.objects.filter(name=username, password=password)

		if user_obj_li.first():

			obj = user_obj_li.values('userPosition__auth__url', 'userPosition__auth__name')
			for i in obj:
				print(i.get('userPosition__auth__url'), i.get('userPosition__auth__name'))      ##跨表拿到了authurlname
				
		else:
			error = "用户名或密码不正确!"
			return render(request, 'login.html', locals())

	return HttpResponse("登录成功!")

访问输出:

Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
增加权限 /auth/add.html
更新权限 /auth/(\d+)/update.html
删除权限 /auth/(\d+)/del.html
查看权限 /auth/list.html
查看主机 /auth/host/list.html
更新主机 /auth/host/(\d+)/update.html
删除主机 /auth/host/(\d+)/del.html
增加主机 /auth/host/add.html
更新权限组 /auth/authgroup/(\d+)/update.html
删除权限组 /auth/authgroup/(\d+)/del.html
增加权限组 /auth/authgroup/add.html
查看权限组 /auth/authgroup/list.html
删除菜单 /auth/menu/(\d+)/del.html
更新菜单 /auth/menu/(\d+)/update.html
查看菜单 /auth/menu/list.html
增加菜单 /auth/menu/add.html
删除用户 /auth/user/(\d+)/del.html
更新用户 /auth/user/(\d+)/update.html
增加用户 /auth/user/add.html
查看用户 /auth/user/list.html

如上的这些数据就是未来我们需要用到根据不同的url去划分不同的权限,但是如上数据虽然拿出来了,但是不是我们所需要的格式,最终我们所需的格式为一个字典格式,key也就是我们的权限作为母菜单,通过循环得到二级菜单等:

mark

赞 (0)

发表评论