Create a simple asset management platform using Django1.11
1: first create a django project
[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: continue to create an 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
Note here that settings.py is a global configuration, that is, all the global configurations under the project are here, and the urls.py to be mentioned below is similar.
3: configure the global setting file
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__)
# variable, the path is the initial path / opt/ops of our project
# 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
# We enable debug to facilitate troubleshooting, and not many people use the operation and maintenance system, so we don't need the support of the third party's Apache-like HTTP service.
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'
# to add our APP name
]
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'
This class allows us to synchronize the language shown at the front end of DJANGO with the system.
'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'
# the above line means to synchronize the Django language with the system
]
}
}
]
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
# enter the database information you are connected to
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'
# modify the time zone, otherwise the time of Django will be inconsistent with that of our system
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: enable urls.py
From django.conf.urls import url
From django.contrib import admin
Urlpatterns = [
Url (r'^ admin/', admin.site.urls)
]
5: models under APP
#-*-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' hostname')
User=models.CharField (max_length=50,verbose_name=u' user')
Brand=models.CharField (max_length=50,verbose_name=u' Brand')
Sn=models.CharField (max_length=50,verbose_name=u'SN')
Mac=models.CharField (max_length=50,blank=True,null=True,verbose_name=u'MAC)
Address')
Os=models.CharField (max_length=50,verbose_name=u' system version')
Cpu=models.CharField (max_length=50,verbose_name=u'CPU')
Memory=models.CharField (max_length=50,verbose_name=u' memory')
Desk=models.CharField (max_length=50,verbose_name=u' hard disk')
Status=models.CharField (blank=True,null=True,verbose_name=u' status')
Remark=models.CharField (blank=True,null=True,verbose_name=u' comment')
Dept=models.CharField (max_length=50,verbose_name=u' Department')
Def _ unicode__ (self):
Return uplink% 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: admin.py under APP
#-*-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: create the database and grant permissions (the process of installing mysql is not described in detail)
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: synchronize the database
[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: open the site to enter 127.0.0.1/admin and log in using the user you just created