1. 程式人生 > >使用Django1.11創建簡單的資產管理平臺

使用Django1.11創建簡單的資產管理平臺

django

1:首先創建一個django項目

[[email protected] opt]# django-admin startproject ops
CommandError: ‘/opt/ops‘ already exists
[[email protected] opt]# cd ops
[[email protected] ops]# tree
.
├── manage.py
└── ops
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

1 directory, 5 files
[[email protected] ops]#

2:繼續創建一個APP

[[email protected] ops]# django-admin startapp polls
[[email protected] 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 [email protected] identified by ‘ops‘;
Query OK, 0 rows affected (0.06 sec)

mysql>

8:同步數據庫

[[email protected] ops]# python manage.py makemigrations polls
Migrations for ‘polls‘:
polls/migrations/0001_initial.py
- Create model ServerList
[[email protected] ops]# python manage.py migrate polls
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0001_initial... OK

9:

[[email protected] ops]# python manage.py createsuperuser
Username (leave blank to use ‘root‘):
Email address: [email protected]
Password:
Password (again):

10:打開站點進入127.0.0.1/admin,使用剛才創建的用戶登錄


本文出自 “擱淺丶” 博客,請務必保留此出處http://yasar.blog.51cto.com/9120455/1923237

使用Django1.11創建簡單的資產管理平臺