首页 > 代码库 > 使用Django1.11创建简单的资产管理平台
使用Django1.11创建简单的资产管理平台
1:首先创建一个django项目
[root@localhost opt]# django-admin startproject ops
CommandError: ‘/opt/ops‘ already exists
[root@localhost opt]# cd ops
[root@localhost ops]# tree
.
├── manage.py
└── ops
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
[root@localhost ops]#
2:继续创建一个APP
[root@localhost ops]# django-admin startapp polls
[root@localhost ops]# tree
.
├── manage.py
├── ops
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── polls
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
3 directories, 12 files
这里要注意下settings.py是全局的配置,即项目下的所有全局配置都在这里,下面要说的urls.py也类似
3:配置全局的setting文件
"""
Django settings for ops project.
Generated by ‘django-admin startproject‘ using Django 1.11.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://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__)))
# 变量,路径是我们项目的初始路径/opt/ops
# 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 = ‘2z7^*hpuui22lg-qly)-%j4$##5w3wy4ike7ow-0p8o#2v^6tx‘
# SECURITY WARNING: don‘t run with debug turned on in production!
DEBUG = True
# 我们开启debug方便排错,加上运维系统使用的人也不多,完全不需要第3方类似Apache的HTTP服务的支持。
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘polls‘,
# 要加入我们的APP名称
]
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‘,
# 这个类让我们可以使得DJANGO的在前端展示的语言和系统同步。
‘django.middleware.locale.LocaleMiddleware‘,
]
ROOT_URLCONF = ‘ops.urls‘
TEMPLATES = [
{
‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,
‘DIRS‘: [],
‘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‘,
‘django.middleware.locale.LocaleMiddleware‘,
# 上面这行表示使Django前段与系统同步
],
},
},
]
WSGI_APPLICATION = ‘ops.wsgi.application‘
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
# DATABASES = {
# ‘default‘: {
# ‘ENGINE‘: ‘django.db.backends.sqlite3‘,
# ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
# }
#}
# connet Mysql Database
# 填写所连接的数据库信息
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘ops‘,
‘USER‘: ‘ops‘,
‘PASSWORD‘: ‘ops‘,
‘HOST‘: ‘localhost‘,
‘PORT‘: ‘3306‘
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_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‘
# 修改时区,不然Django的时间和我们系统时间会不一致
TIME_ZONE = ‘Asia/Shanghai‘
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = ‘/static/‘
4: 启用urls.py
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
]
5: APP下的models
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class ServerList(models.Model):
hostname=models.CharField(max_length=50,verbose_name=u‘主机名‘)
user=models.CharField(max_length=50,verbose_name=u‘使用人‘)
brand=models.CharField(max_length=50,verbose_name=u‘品牌‘)
sn=models.CharField(max_length=50,verbose_name=u‘SN‘)
mac=models.CharField(max_length=50,blank=True,null=True,verbose_name=u‘MAC地
址‘)
os=models.CharField(max_length=50,verbose_name=u‘系统版本‘)
cpu=models.CharField(max_length=50,verbose_name=u‘CPU‘)
memory=models.CharField(max_length=50,verbose_name=u‘内存‘)
desk=models.CharField(max_length=50,verbose_name=u‘硬盘‘)
status=models.CharField(blank=True,null=True,verbose_name=u‘状态‘)
remark=models.CharField(blank=True,null=True,verbose_name=u‘注释‘)
dept=models.CharField(max_length=50,verbose_name=u‘部门‘)
def __unicode__(self):
return u‘%s %s %s %s %s %s %s %s %s %s %s %s‘ %(self.hostname,self.dept,self.user,self.brand,self.os,self.cpu,self.memory,self.desk,self.mac,self.sn,self.status,self.remark)
6: APP下的admin.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from polls.models import ServerList
class TitleList(admin.ModelAdmin):
list_display = (‘hostname‘,‘user‘,‘brand‘,‘sn‘,‘mac‘,‘os‘,‘cpu‘,‘memory‘,‘desk‘,‘status‘,‘remark‘,‘dept‘)
search_fields = (‘hostname‘,‘user‘,‘brand‘,‘sn‘,‘mac‘,‘os‘,‘cpu‘,‘memory‘,‘desk‘,‘status‘,‘remark‘,‘dept‘)
admin.site.register(ServerList,TitleList)
7:创建数据库并赋予权限(安装mysql的过程就不详细介绍)
mysql> CREATE DATABASE `ops` /*!40100 DEFAULT CHARACTER SET utf8 */;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON `ops`.* TO ‘ops‘@‘localhost‘ identified by ‘ops‘;
Query OK, 0 rows affected (0.06 sec)
mysql>
8:同步数据库
[root@localhost ops]# python manage.py makemigrations polls
Migrations for ‘polls‘:
polls/migrations/0001_initial.py
- Create model ServerList
[root@localhost ops]# python manage.py migrate polls
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0001_initial... OK
9:
[root@localhost ops]# python manage.py createsuperuser
Username (leave blank to use ‘root‘):
Email address: mail@mail.com
Password:
Password (again):
10:打开站点进入127.0.0.1/admin,使用刚才创建的用户登录
本文出自 “搁浅丶” 博客,请务必保留此出处http://yasar.blog.51cto.com/9120455/1923237
使用Django1.11创建简单的资产管理平台
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。