分类: Python/Ruby
2013-09-11 17:06:09
5.django ADMIN 的启用
5.1 激活管理页面
root@ubuntu:/opt/serManager/serManager# vim settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'sshCMD'
)
5.2 url设置
root@ubuntu:/opt/serManager/serManager# vim urls.py
from django.conf.urls import patterns, include, url
#add by hans 20130906
from sshCMD.views import index
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'serManager.views.home', name='home'),
# url(r'^serManager/', include('serManager.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^$',index),
(r'bootstrap/(?P
5.3 创建编辑admin
root@ubuntu:/opt/serManager/sshCMD# vim admin.py
from django.contrib import admin
from sshCMD.models import SerInfo
admin.site.register(SerInfo)
运行虚拟机,查看管理界面
root@ubuntu:/opt/serManager# python
manage.py runserver 0.0.0.0:8080
5.4 为了方便管理,能分清不同的记录,这里定义unicode区分
from django.db import models
class SerInfo(models.Model):
gid = models.IntegerField()
user = models.CharField(max_length=30)
password = models.CharField(max_length=30)
ser_name = models.CharField(max_length=30)
ip = models.CharField(max_length=30)
key = models.BooleanField()
cmd = models.CharField(max_length=60)
email = models.EmailField(max_length=75)
status = models.BooleanField()
datetime = models.DateField()
def
__unicode__(self):
return u'%s %s
%s'%(self.ip,self.user,self.datetime)
5.5 自定义列表
root@ubuntu:/opt/serManager/sshCMD# vim
admin.py
from django.contrib import admin
from sshCMD.models import SerInfo
class SerInfoAdmin(admin.ModelAdmin):
list_display =
('gid','user','ip')
admin.site.register(SerInfo,SerInfoAdmin)
过滤器的使用
from django.contrib import admin
from sshCMD.models import SerInfo
class SerInfoAdmin(admin.ModelAdmin):
list_display = ('gid','user','ip')
search_fields = ('gid','ip')
list_filter =
('ser_name',)
admin.site.register(SerInfo,SerInfoAdmin)
表单的自定义:
from django.contrib import admin
from sshCMD.models import SerInfo
class SerInfoAdmin(admin.ModelAdmin):
list_display = ('gid','user','ip')
search_fields = ('gid','ip')
list_filter = ('ser_name',)
fields =
('ip','ser_name','user','cmd','email','gid','datetime')
admin.site.register(SerInfo,SerInfoAdmin)
---------------------------------------------------------