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

Django框架–权限系统

通过之前的课程,目前index中已经可以针对不同的用户得到不同的菜单选项,下面我们把host的数据补充到index页面:

views.py

from django.shortcuts import render,HttpResponse,redirect

# Create your views here.
from auth_server import models
from utils import login_auth
from django.views import View


def auth_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():

			###用户名,密码输入正确,初始化数据:
			login_auth.menu_auth(user_obj_li, request)
			###跳转到index页面:
			print("123123")
			# return redirect('/index.html')
			return redirect('/auth/list')
		else:
			###用户名或密码错误:
			print("345345")
			error = "用户名或密码错误!!"
			return render(request, 'login.html',locals())

### 自定义如上index
def index(request):
	menu_dic = request.session.get('menu_dic')
	print("666666")
	print("menu_dic--->", menu_dic)
	return render(request, 'index.html', {'menu_dic': menu_dic})
	# return redirect('/auth/list')


class List(View):
	def post(self, request, *args, **kwargs):
		pass

	def get(self, request, *args, **kwargs):
		menu_dic = request.session.get('menu_dic')		##注意此句  前端页面需要调用的数据
		host_list = models.Host.objects.all()
		# print(host_list)
		return render(request, 'host.html', locals())

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>

<div id="index">
    {% for item in menu_dic.values %}
        <h2>{{ item.name }}</h2>
        {% for i in item.lower %}
            <div>
                <a href="{{ i.name }}"> {{ i.url }} </a>
            </div>
        {% endfor %}
    {% endfor %}

{% block content %}

{% endblock %}
</div>
</body>
</html>

host.html

{% extends "index.html" %}

{% block content %}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>主机</title>
    </head>
    <body>

    <div class="container">
        <table class="table table-bordered table-hover" id="table" border="1">
            <thead>
            <tr>
                <th style="text-align: center"> </th>
                <th style="text-align: center">主机名</th>
                <th style="text-align: center">实例ID</th>
                <th style="text-align: center">CPU</th>
                <th style="text-align: center">内存/G</th>
                <th style="text-align: center">带宽/M</th>
                <th style="text-align: center">IP</th>
                <th style="text-align: center">来源类型</th>
                <th style="text-align: center">所属区域</th>
                <th style="text-align: center">状态</th>
                <th style="text-align: center"> </th>
            </tr>
            </thead>

            <tbody>
            {% for host in host_list %}
                <tr>
                    <td style="text-align: center">{{ forloop.counter }}</td>
                    <td style="text-align: center">{{ host.hostname }}</td>
                    <td style="text-align: center">{{ host.ecsname }}</td>
                    <td style="text-align: center">{{ host.cpu }}</td>
                    <td style="text-align: center">{{ host.mem }}</td>
                    <td style="text-align: center">{{ host.speed }}</td>
                    {#                    <td style="text-align: center"><a href="/search/?mem={{ item.mem_info }}">{{ item.mem_info }}</a>#}
                    {#                    </td>#}
                    <td style="text-align: center">{{ host.network }}</td>
                    <td style="text-align: center">{{ host.source.name }}</td>
                    <td style="text-align: center">{{ host.region.name }}</td>
                    <td style="text-align: center">{{ host.get_state_display }}</td>
                    <td style="text-align: center">
                        <a href="/host/del?id={{ host.id }}">删除</a> | <!--a标签是GET请求-->
                        <a href="/host/edit/{{ host.id }}">编辑</a> |
                        <a href="/host/add">增加</a>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    </body>
    </html>

{% endblock %}

最终当我们访问成功,进入 /auth/list

mark

赞 (0)

发表评论