博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【django小练习之主机管理界面】
阅读量:5135 次
发布时间:2019-06-13

本文共 13469 字,大约阅读时间需要 44 分钟。

需求:

利用django,js,bootstrap等实现登录,主机管理等操作。

 

实现截图

  • 登录界面

 

  • 主机界面,添加及编辑

  • 部门管理界面

 

代码实现

  • 目录层级

 

 

settings.py

"""Django settings for day16 project.Generated by 'django-admin startproject' using Django 1.11.4.For more information on this file, seehttps://docs.djangoproject.com/en/1.11/topics/settings/For the full list of settings and their values, seehttps://docs.djangoproject.com/en/1.11/ref/settings/"""import os# Build paths inside the project like this: os.path.join(BASE_DIR, ...)BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Quick-start development settings - unsuitable for production# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!SECRET_KEY = 'l))&0*l6$aja*lcq8=0-s9u4byl2%alzfsgdxs_&3_qre&=mvw'# SECURITY WARNING: don't run with debug turned on in production!DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'app01.apps.App01Config',]MIDDLEWARE = [    'django.middleware.security.SecurityMiddleware',    'django.contrib.sessions.middleware.SessionMiddleware',    'django.middleware.common.CommonMiddleware',    # 'django.middleware.csrf.CsrfViewMiddleware',    'django.contrib.auth.middleware.AuthenticationMiddleware',    'django.contrib.messages.middleware.MessageMiddleware',    'django.middleware.clickjacking.XFrameOptionsMiddleware',]ROOT_URLCONF = 'day16.urls'TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        'DIRS': [os.path.join(BASE_DIR, 'templates')]        ,        'APP_DIRS': True,        'OPTIONS': {            'context_processors': [                'django.template.context_processors.debug',                'django.template.context_processors.request',                'django.contrib.auth.context_processors.auth',                'django.contrib.messages.context_processors.messages',            ],        },    },]WSGI_APPLICATION = 'day16.wsgi.application'# Database# https://docs.djangoproject.com/en/1.11/ref/settings/#databasesDATABASES = {    'default': {        'ENGINE': 'django.db.backends.sqlite3',        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),    }}# Password validation# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validatorsAUTH_PASSWORD_VALIDATORS = [    {        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',    },    {        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',    },    {        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',    },    {        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',    },]# Internationalization# https://docs.djangoproject.com/en/1.11/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/1.11/howto/static-files/STATIC_URL = '/static/'STATICFILES_DIRS = (    os.path.join(BASE_DIR,'static'),)
settings.py

 

urls.py

"""day16 URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:    https://docs.djangoproject.com/en/1.11/topics/http/urls/Examples:Function views    1. Add an import:  from my_app import views    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')Class-based views    1. Add an import:  from other_app.views import Home    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')Including another URLconf    1. Import the include() function: from django.conf.urls import url, include    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))"""from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^login/',views.login),    url(r'^index/',views.index),    url(r'^parts/',views.parts),    url(r'^part_add/',views.part_add),    url(r'^part_del/',views.part_del),    url(r'^part_edit/',views.part_edit),    url(r'^hosts/',views.hosts),    url(r'^hosts_add/',views.hosts_add),    url(r'^hosts_del/',views.hosts_del),    url(r'^hosts_edit/',views.hosts_edit),]
urls.py

 

models.py

from django.db import models# Create your models here.from django.db import modelsclass UserInfo(models.Model):    id = models.AutoField(primary_key=True)    user = models.CharField(max_length=32)    pwd = models.CharField(max_length=64)    age = models.IntegerField()class Department(models.Model):    id = models.AutoField(primary_key=True)    title = models.CharField(max_length=32)class Hosts(models.Model):    id = models.AutoField(primary_key=True)    hostname = models.CharField(max_length=64)    ip = models.CharField(max_length=64)    depart = models.ForeignKey(to='Department',to_field='id',default=1)
models.py

 

views.py

from django.shortcuts import render,HttpResponse,redirect# Create your views here.from app01 import models# def test(request):#     # #查询所有用户信息#     # user_list = models.UserInfo.objects.all()#     # for obj in user_list:#     #     print(obj,obj.id,obj.user,obj.pwd)#     ##     # return HttpResponse('123')#     ##     # user_list = models.UserInfo.objects.filter(user='alex',pwd='123').all#     # print(user_list)#     ##     # user = models.UserInfo.objects.filter(user='alex',pwd='123').first()#     # print(user,user.id,user.user,user.pwd)#     return HttpResponse('123')def login(request):    if request.method == "GET":        #打开login.html文件        #找到特殊标记{
{msg}} #并将第三个参数中字典总的对应值替换 #将替换完毕的字符串发送给用户浏览器 return render(request,"login.html",{
'msg':''}) else: #去请求体中获取数据 username1 = request.POST.get("username") password1 = request.POST.get("password") print(username1,password1) userinfo = models.UserInfo.objects.filter(user=username1,pwd=password1).first() print(userinfo) if userinfo: # return redirect('http://www.baidu.com') return redirect('/index/') else: return render(request,'login.html',{
'msg':'用户名或密码错误'}) # if user == "alex" and pwd == "123": # #在响应头中设置,location:http://www.baidu.com,无响应体 # return redirect('http://www.baidu.com') # #return redirect('/index/') # else: # return render(request,'login.html',{'msg':'用户名或密码错误'})def index(request): return render(request,'index.html')def parts(request): depart_list = models.Department.objects.all() return render(request,'parts.html',{
'depart_list':depart_list})def part_add(request): if request.method == 'GET': return render(request,'part_add.html') else: ti = request.POST.get('title') models.Department.objects.create(title=ti) return redirect('/parts/')def part_del(request): nid = request.GET.get('nid') models.Department.objects.filter(id=nid).delete() return redirect('/parts/')def part_edit(request): if request.method == 'GET': nid = request.GET.get('nid') obj = models.Department.objects.filter(id=nid).first() if not obj: return HttpResponse('写错了,返回吧') return render(request,'part_edit.html',{
'obj':obj}) else: nid = request.GET.get('nid') title = request.POST.get('title') models.Department.objects.filter(id=nid).update(title=title) return redirect('/parts')def hosts(request): hosts_list = models.Hosts.objects.all() return render(request,'hosts.html',{
'hosts_list':hosts_list})def hosts_add(request): if request.method == 'GET': depart_list = models.Department.objects.all() return render(request,'hosts_add.html',{
'depart_list':depart_list}) else: hostname1 = request.POST.get('hostname') ip1 = request.POST.get('ip') dp_id = request.POST.get('dp_id') models.Hosts.objects.create(hostname=hostname1,ip=ip1,depart_id=dp_id) return redirect('/hosts')def hosts_del(request): nid = request.GET.get('nid') models.Hosts.objects.filter(id=nid).delete() return redirect('/hosts')def hosts_edit(request): if request.method == 'GET': nid = request.GET.get('nid') obj = models.Hosts.objects.filter(id=nid).first() depart_list = models.Department.objects.all() if not obj: return HttpResponse('写错了,返回吧') return render(request,'hosts_edit.html',{
'obj':obj,'depart_list':depart_list}) else: nid = request.GET.get('nid') hostname1 = request.POST.get('hostname') ip1 = request.POST.get('ip') dp_id = request.POST.get('dp_id') models.Hosts.objects.filter(id=nid).update(hostname=hostname1,ip=ip1,depart_id=dp_id) return redirect('/hosts')
views.py

 

login.html

    
Title

登录界面

{
{ msg }}
login.html

 

index.html

    
Title

欢迎登录

index.html

 

parts.html

    
Title

部门列表

{% for obj in depart_list %}
{% endfor %}
ID 部门 操作
{
{ obj.id }}
{
{ obj.title }}
编辑 删除
parts.html

 

parts_add.html

    
Title

添加部门

parts_add.html

 

parts_edit.html

    
Title

编辑部门

parts_edit.html

 

hosts.html

    
Title

主机列表

{% for obj in hosts_list %}
{% endfor %}
ID 主机名 ip 部门id 所属部门 操作
{
{ obj.id }}
{
{ obj.hostname }}
{
{ obj.ip}}
{
{ obj.depart_id}}
{
{ obj.depart.title }}
编辑 删除
hosts.html

 

hosts_add.html

    
Title

添加主机

hosts_add.html

 

hosts_edit.html

    
Title

编辑主机信息

hosts_edit.html

 

转载于:https://www.cnblogs.com/smallmars/p/8353507.html

你可能感兴趣的文章
Bitmap 算法
查看>>
转载 C#文件中GetCommandLineArgs()
查看>>
list control控件的一些操作
查看>>
精读《useEffect 完全指南》
查看>>
SNF快速开发平台MVC-EasyQuery-拖拽生成SQL脚本
查看>>
DrawerLayout实现双向侧滑
查看>>
MySQL入门很简单-触发器
查看>>
LVM快照(snapshot)备份
查看>>
绝望的第四周作业
查看>>
一月流水账
查看>>
数论四大定理
查看>>
npm 常用指令
查看>>
20几个正则常用正则表达式
查看>>
TextArea中定位光标位置
查看>>
非常棒的Visual Studo调试插件:OzCode 2.0 下载地址
查看>>
判断字符串在字符串中
查看>>
hdu4374One hundred layer (DP+单调队列)
查看>>
类间关系总结
查看>>
properties配置文件读写,追加
查看>>
Linux环境下MySql安装和常见问题的解决
查看>>