一、models
在app/models.py
1、django.db.models
任何model的class都继承这个类
from django.db import models
class Server(models.Model):
serverName = models.CharField(max_length = 36,verbose_name="服务器名称")
serverStatus = models.ForeignKey(Status)
createtime= models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
remark = models.CharField(max_length=64,verbose_name="备注")
def __unicode__(self):
return self.serverName
2、django.contrib.admin
管理设计的class,需要针对基本的类设计一个管理类,设置显示的项和过滤的项目
from django.contrib import admin
class ServerAdmin(admin.ModelAdmin):
list_display = ('serverName','serverStatus','createti
list_filter = ('serverName','serverStatus','remark')
需要将基本类注册到管理类
admin.site.register(Server,ServerAdmin)
二、URL
django1.4在 项目/项目/urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls import *
from django.views.generic.simple import direct_to_template#通用视图的跳转,可以跳转到一个template或者到一个视图
from django.contrib import admin #使用admin功能
admin.autodiscover() #使用admin功能
import settings #导入配置文件中静态文件的配置
#import game #导入app中指定的视图
#from game import dbbackup_list,DBBackup_insert #导入app中指定的视图
# -*- coding: UTF-8 -*-
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
from django.contrib import admin
import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^resources/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.STATIC_PATH}),
)
urlpatterns += patterns('game.views',
(r'^index/$', direct_to_template, {'template': 'index.html'}),
#(r'^serverstatus_list/$','serverstatus_list',{'template_name': 'serverstatus_list.html'}),
#(r'^total/(?P<optype>[^/]+)/','total_list',{'template_name': 'total_list.html'}),
(r'^report/$','Game_Report',{'template_name': 'report.html'}),
(r'^dbupdate/list/$','dbupdate_list',{'template_name': 'dbupdate_list.html'}),
(r'^dbupdate/add/(?P<servername>[^/]+)/(?P<status>[^/]+)/(?P<remark>[^/]+)/$','DBUpdate_insert',{'template_name': 'game_insert.html'}),
(r'^codeupdate/list/$','codeupdate_list',{'template_name': 'codeupdate_list.html'}),
(r'^codeupdate/add/(?P<servername>[^/]+)/(?P<status>[^/]+)/(?P<remark>[^/]+)/$','CodeUpdate_insert',{'template_name': 'game_insert.html'}),
(r'^dbbackup/list/$','dbbackup_list',{'template_name': 'dbbackup_list.html'}),
(r'^dbbackup/add/(?P<servername>[^/]+)/(?P<level>[^/]+)/(?P<status>[^/]+)/(?P<remark>[^/]+)/$','DBBackup_insert',{'template_name': 'game_insert.html'}),
)
三、views
# vim game/views.py
# -*- coding: UTF-8 -*-
# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404 #一般返回和返回404
from django.core.paginator import PageNotAnInteger,Paginator,InvalidPage,EmptyPage #分页相关
from django.views.generic import list_detail#
from django.db.models import Q #多个字段联合查询
from time import strftime
from django.forms.models import model_to_dict #qset转换为dict
from game.models import DBUpdate,CodeUpdate,DBBackup,Domain,Status,Level
from game.ServerForm import statusForm,dbbackupForm
import simplejson#json格式
1、HttpResponse("Hello world")
from django.shortcuts import HttpResponse
直接返回信息或者返回tempate
2、render_to_response
from django.shortcuts import render_to_response
可以自定义返回的template和数据
return render_to_response(template_name,{'record_list':server_list,'page_range':page_range,'error':error,'form':form})
四、数据库操作
http://blog.csdn.net/shanliangliuxing/article/details/7652646
1、插入数据
>>> from books.models import Publisher
#导入Publisher模型类
>>> p1 = Publisher(name='Apress', address='2855 Telegraph Avenue', city='Berkeley', state_province='CA', country='U.S.A.',website='http://www.apress.com/')
#创建Publisher类的实例p1
>>> p1.save()
#调用实例p1的save()方法将数据存入数据库,Django 会在后台执行一条 INSERT 语句。
简写:
DBUpdate(serverName=servername,UpdateStatus=status,remark=remark).save()
其他一步创建的方法: objects.create()
p1 = Publisher.objects.create(name='Apress', address='2855 Telegraph Avenue', city='Berkeley', state_province='CA', country='U.S.A.',website='http://www.apress.com/')
2、获取数据 查询
1)、获取所有objects.all()
注意获取到的是一个QuerySet对象
publisher_list = Publisher.objects.all()
2)、objects.filter()
数据过滤 Publisher.objects.filter()
传递多个参数到 filter() 来缩小选取范围:
Publisher.objects.filter(country="U.S.A.", state_province="CA")
3)、获取的单个对象objects.get()
>>> Publisher.objects.get(name="Apress")
4)、数据排序 objects.order_by()
Publisher.objects.order_by("name")
还可以指定逆向排序,在前面加一个减号 - 前缀:
5)、在模型的缺省排序方式
class Publisher(models.Model):
name = models.CharField(max_length=30)
'''code'''
class Meta:
ordering = ['name']
6)、连锁查询objects.filter().order_by() #过滤后再排序
Publisher.objects.filter().order_by()
7)、限制返回的数据 使用标准的Python列表裁剪语句
Publisher.objects.order_by('name')[0]
3、更新对象
>>> p = Publisher.objects.get(name='Apress')
>>> p.name = 'Apress Publishing'
>>> p.save()
使用update()方法
Publisher.objects.filter(id=52).update(name='Apress Publishing')
4、删除对象
删除数据库中的对象只需调用该对象的delete()方法即可:
>>> p = Publisher.objects.get(name="jin")
>>> p.delete()
同样我们可以在结果集上调用delete()方法同时删除多条记录。这一点与update()方法相似:
>>> Publisher.objects.filter(country='USA').delete() #有条件删除
>>> Publisher.objects.all().delete() #删除所有数据