使用Django1.11创建简单的资产管理平台

1:首先创建一个django项目

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" >[root@localhost opt] # django-admin startproject ops<br>CommandError: '/opt/ops' already exists<br>[root@localhost opt]# cd ops<br>[root@localhost ops]# tree<br>.<br>├── manage.py<br>└── ops<br>    ├── __init__.py<br>    ├── settings.py<br>    ├── urls.py<br>    └── wsgi.py<br><br>1 directory, 5 files<br>[root@localhost ops]#<br></span>

2:继续创建一个APP

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" >[root@localhost ops] # django-admin startapp polls<br>[root@localhost ops]# tree<br>.<br>├── manage.py<br>├── ops<br>│   ├── __init__.py<br>│   ├── settings.py<br>│   ├── urls.py<br>│   └── wsgi.py<br>└── polls<br>    ├── admin.py<br>    ├── apps.py<br>    ├── __init__.py<br>    ├── migrations<br>    │   └── __init__.py<br>    ├── models.py<br>    ├── tests.py<br>    └── views.py<br><br>3 directories, 12 files<br>这里要注意下settings.py是全局的配置,即项目下的所有全局配置都在这里,下面要说的urls.py也类似<br></span>

3:配置全局的setting文件

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" >Django settings  for  ops project.<br><br>Generated by  'django-admin startproject'  using Django  1.11 .<br><br>For more information on this  file , see<br>https: / / docs.djangoproject.com / en / 1.11 / topics / settings / <br><br>For the full  list  of settings  and  their values, see<br>https: / / docs.djangoproject.com / en / 1.11 / ref / settings / <br><br><br> import  os<br><br> # Build paths inside the project like this: os.path.join(BASE_DIR, ...)<br>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))<br># 变量,路径是我们项目的初始路径/opt/ops<br><br># Quick-start development settings - unsuitable for production<br># See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/<br><br># SECURITY WARNING: keep the secret key used in production secret!<br>SECRET_KEY = '2z7^*hpuui22lg-qly)-%j4$##5w3wy4ike7ow-0p8o#2v^6tx'<br><br># SECURITY WARNING: don't run with debug turned on in production!<br>DEBUG = True<br># 我们开启debug方便排错,加上运维系统使用的人也不多,完全不需要第3方类似Apache的HTTP服务的支持。<br><br>ALLOWED_HOSTS = []<br><br><br># Application definition<br><br>INSTALLED_APPS = [<br>    'django.contrib.admin',<br>    'django.contrib.auth',<br>    'django.contrib.contenttypes',<br>    'django.contrib.sessions',<br>    'django.contrib.messages',<br>    'django.contrib.staticfiles',<br>    'polls',<br>    # 要加入我们的APP名称<br>]<br><br>MIDDLEWARE = [<br>    'django.middleware.security.SecurityMiddleware',<br>    'django.contrib.sessions.middleware.SessionMiddleware',<br>    'django.middleware.common.CommonMiddleware',<br>    'django.middleware.csrf.CsrfViewMiddleware',<br>    'django.contrib.auth.middleware.AuthenticationMiddleware',<br>    'django.contrib.messages.middleware.MessageMiddleware',<br>    'django.middleware.clickjacking.XFrameOptionsMiddleware',<br>    # 这个类让我们可以使得DJANGO的在前端展示的语言和系统同步。<br>    'django.middleware.locale.LocaleMiddleware',<br>]<br><br>ROOT_URLCONF = 'ops.urls'<br><br>TEMPLATES = [<br>    {<br>        'BACKEND': 'django.template.backends.django.DjangoTemplates',<br>        'DIRS': [],<br>        'APP_DIRS': True,<br>        'OPTIONS': {<br>            'context_processors': [<br>                'django.template.context_processors.debug',<br>                'django.template.context_processors.request',<br>                'django.contrib.auth.context_processors.auth',<br>                'django.contrib.messages.context_processors.messages',<br>                'django.middleware.locale.LocaleMiddleware',<br>                # 上面这行表示使Django前段语言与系统同步         <br>            ],<br>        },<br>    },<br>]<br><br>WSGI_APPLICATION = 'ops.wsgi.application'<br><br><br># Database<br># https://docs.djangoproject.com/en/1.11/ref/settings/#databases<br><br>#  DATABASES = {<br>#    'default': {<br>#        'ENGINE': 'django.db.backends.sqlite3',<br>#        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),<br>#    }<br>#}<br><br># connet Mysql Database<br># 填写所连接的数据库信息<br>DATABASES = {<br>    'default': {<br>        'ENGINE': 'django.db.backends.mysql',<br>        'NAME': 'ops',<br>        'USER': 'ops',<br>        'PASSWORD': 'ops',<br>        'HOST': 'localhost',<br>        'PORT': '3306'<br>        }<br>}<br><br># Password validation<br># https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators<br><br>AUTH_PASSWORD_VALIDATORS = [<br>    {<br>        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',<br>    },<br>    {<br>        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',<br>    },<br>    {<br>        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',<br>    },<br>    {<br>        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',<br>    },<br>]<br><br><br># Internationalization<br># https://docs.djangoproject.com/en/1.11/topics/i18n/<br><br>LANGUAGE_CODE = 'en-us'<br><br># TIME_ZONE = 'UTC'<br># 修改时区,不然Django的时间和我们系统时间会不一致<br>TIME_ZONE = 'Asia/Shanghai'<br><br>USE_I18N = True<br><br>USE_L10N = True<br><br>USE_TZ = True<br><br><br># Static files (CSS, JavaScript, Images)<br># https://docs.djangoproject.com/en/1.11/howto/static-files/<br><br>STATIC_URL = '/static/'<br></span>

4: 启用urls.py

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" > from  django.conf.urls  import  url<br> from  django.contrib  import  admin<br><br>urlpatterns  =  [<br>    url(r '^admin/' , admin.site.urls),<br>]<br>< / span>

5: APP下的models

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" > # -*- coding: utf-8 -*-<br><br>from __future__ import unicode_literals<br><br>from django.db import models<br><br># Create your models here.<br>class ServerList(models.Model):<br>    hostname=models.CharField(max_length=50,verbose_name=u'主机名')<br>    user=models.CharField(max_length=50,verbose_name=u'使用人')<br>    brand=models.CharField(max_length=50,verbose_name=u'品牌')<br>    sn=models.CharField(max_length=50,verbose_name=u'SN')<br>    mac=models.CharField(max_length=50,blank=True,null=True,verbose_name=u'MAC地<br>址')<br>    os=models.CharField(max_length=50,verbose_name=u'系统版本')<br>    cpu=models.CharField(max_length=50,verbose_name=u'CPU')<br>    memory=models.CharField(max_length=50,verbose_name=u'内存')<br>    desk=models.CharField(max_length=50,verbose_name=u'硬盘')<br>    status=models.CharField(blank=True,null=True,verbose_name=u'状态')<br>    remark=models.CharField(blank=True,null=True,verbose_name=u'注释')<br>    dept=models.CharField(max_length=50,verbose_name=u'部门')<br><br>    def __unicode__(self):<br>        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)<br></span>

6: APP下的admin.py

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" > # -*- coding: utf-8 -*-<br>from __future__ import unicode_literals<br><br>from django.contrib import admin<br>from polls.models import ServerList<br>class TitleList(admin.ModelAdmin):<br>    list_display = ('hostname','user','brand','sn','mac','os','cpu','memory','desk','status','remark','dept')<br>    search_fields = ('hostname','user','brand','sn','mac','os','cpu','memory','desk','status','remark','dept')<br>admin.site.register(ServerList,TitleList)<br></span>

7:创建数据库并赋予权限(安装mysql的过程就不详细介绍)

1
<span style= "font-family:'宋体', SimSun;font-size:14px;" >mysql> CREATE DATABASE `ops` /*!40100 DEFAULT CHARACTER SET utf8 */;<br>Query OK, 1 row affected (0.00 sec)<br><br>mysql> GRANT ALL PRIVILEGES ON `ops`.* TO  'ops' @ 'localhost'  identified by  'ops' ;<br>Query OK, 0 rows affected (0.06 sec)<br><br>mysql><br>< /span >

8:同步数据库

1
<span style = "font-family:'宋体', SimSun;font-size:14px;" >[root@localhost ops] # python manage.py makemigrations polls<br>Migrations for 'polls':<br>  polls/migrations/0001_initial.py<br>    - Create model ServerList<br>[root@localhost ops]# python manage.py migrate polls<br>Operations to perform:<br>  Apply all migrations: polls<br>Running migrations:<br>  Applying polls.0001_initial... OK<br></span>

9: 

1
<span style= "font-family:'宋体', SimSun;font-size:14px;" >[root@localhost ops] # python manage.py createsuperuser<br>Username (leave blank to use 'root'):<br>Email address: mail@mail.com<br>Password:<br>Password (again):<br></span>

10:打开站点进入127.0.0.1/admin,使用刚才创建的用户登录




本文转自 运维小学生 51CTO博客,原文链接:http://blog.51cto.com/yasar/1923237,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值