def delhost(request, id): HostObj = HostInfo.objects.get(id=id) HostObj.delete() ret = { 'allServerObj': None, 'UserInfoObj': None, 'PageInfo': None, 'AllCount': None } page = 1 allServer = HostInfo.objects.all() AllCount = allServer.count() ret['AllCount'] = AllCount PageObj = Page(AllCount, page, 6) allServerObj = allServer[PageObj.begin:PageObj.end] pageurl = 'index' pageinfo = page_div(page, PageObj.all_page_count, pageurl) ret['PageInfo'] = pageinfo ret['allServerObj'] = allServerObj UserInfoObj = UserInfo.objects.get( username=request.session.get('username', None)) ret['UserInfoObj'] = UserInfoObj ret['popover'] = {"id": id, "info": "已经删除主机"} return render_to_response('index.html', ret, context_instance=RequestContext(request))
def index(request,page=1): ret = {'ArticleObj':None,'PageInfo':None} try: page = int(page) except Exception: page = 1 AllCount = Article.objects.filter(status='p').all().count() PageObj = Page(AllCount,page,6) #根据主键序号倒序排列 ArticleObj = Article.objects.filter(status='p').order_by('-id').all()[PageObj.begin:PageObj.end] pageurl = 'index' pageinfo = page_div(page, PageObj.all_page_count,pageurl) ret['PageInfo'] = pageinfo ret['ArticleObj'] = ArticleObj return render_to_response('index.html',ret,context_instance=RequestContext(request))
def searchtag(request,tagname,page=1): ret = {'ArticleObj':None,'PageInfo':None,'TagName':None} #根据Article对象的tag字段多对多对应TagInfo表的tagname字段 MatchTagObj = Article.objects.filter(tag__tagname__contains = tagname,status='p') #判断如果传进来的tagname查不出结果就返回404 if not MatchTagObj: return HttpResponseNotFound('<h1>Page not found</h1>') AllCount = MatchTagObj.all().count() #分页为首页 try: page = int(page) except Exception: page = 1 PageObj = Page(AllCount,page,4) ArticleObj = MatchTagObj.order_by('-id').all()[PageObj.begin:PageObj.end] pageurl = 'tag' + '/' + tagname pageinfo = page_div(page, PageObj.all_page_count,pageurl) ret['PageInfo'] = pageinfo ret['ArticleObj'] = ArticleObj ret['TagName'] = tagname return render_to_response('index.html',ret,context_instance=RequestContext(request))
def batchdelhost(request): if request.method == 'POST': #根据传进来的主机id批量删除数据库对象 ret = { 'allServerObj': None, 'UserInfoObj': None, 'PageInfo': None, 'AllCount': None } tmpmeminfo = request.POST.getlist("checkboxdel[]", None) if tmpmeminfo: for i in tmpmeminfo: HostObj = HostInfo.objects.get(id=i) HostObj.delete() ids = ",".join(tmpmeminfo) ret['popover'] = {"id": ids, "info": "已经删除主机"} else: ret['popover'] = {"id": "", "info": "没有选中可删除的主机"} page = 1 allServer = HostInfo.objects.all() AllCount = allServer.count() ret['AllCount'] = AllCount PageObj = Page(AllCount, page, 6) allServerObj = allServer[PageObj.begin:PageObj.end] pageurl = 'index' pageinfo = page_div(page, PageObj.all_page_count, pageurl) ret['PageInfo'] = pageinfo ret['allServerObj'] = allServerObj UserInfoObj = UserInfo.objects.get( username=request.session.get('username', None)) ret['UserInfoObj'] = UserInfoObj return render_to_response('index.html', ret, context_instance=RequestContext(request)) else: return HttpResponseNotFound('<h1>Page not found</h1>')
def index(request, page=1): ret = { 'allServerObj': None, 'UserInfoObj': None, 'PageInfo': None, 'AllCount': None } try: page = int(page) except Exception: page = 1 if request.method == 'GET': #查询页面的分页显示 if request.GET.get('issearch', None): searchos = request.GET.get('searchos', None) searchhostname = request.GET.get('searchhostname', None) searchsn = request.GET.get('searchsn', None) searchpublish = request.GET.get('searchpublish', None) searchip = request.GET.get('searchip', None) tmpstarttime = request.GET.get('searchstarttime', None) tmpendtime = request.GET.get('searchendtime', None) Qset = {} Qset['searchos'] = searchos Qset['searchhostname'] = searchhostname Qset['searchsn'] = searchsn Qset['searchpublish'] = searchpublish Qset['searchip'] = searchip Qset['tmpstarttime'] = tmpstarttime Qset['tmpendtime'] = tmpendtime #判断是否输入了开始时间,没输入或输入非法则默认为1970.01.01 try: searchstarttime = datetime.datetime.strptime( tmpstarttime, '%Y-%m-%d') except: searchstarttime = datetime.datetime(1970, 1, 1) #判断是否输入了结束时间或输入非法,没输入或输入非法则默认为现在 try: searchendtime = datetime.datetime.strptime( tmpendtime, '%Y-%m-%d') except: searchendtime = datetime.datetime.now() allServer = HostInfo.objects( Q(os__contains=searchos) & Q(networkinfo__addrlst__contains=searchip) & Q(hostname__contains=searchhostname) & Q(ispublish__contains=searchpublish) & Q(hardwareinfo__SN__contains=searchsn) & Q(timestamp__gte=searchstarttime) & Q(timestamp__lte=searchendtime)) AllCount = allServer.count() ret['AllCount'] = AllCount PageObj = Page(AllCount, page, 6) allServerObj = allServer[PageObj.begin:PageObj.end] pageurl = 'index' querycondition = request.META.get("QUERY_STRING", None) pageinfo = query_page_div(page, PageObj.all_page_count, pageurl, querycondition) ret['PageInfo'] = pageinfo ret['allServerObj'] = allServerObj UserInfoObj = UserInfo.objects.get( username=request.session.get('username', None)) ret['UserInfoObj'] = UserInfoObj ret['Qset'] = Qset return render_to_response('index.html', ret, context_instance=RequestContext(request)) #正常主页的分页显示 else: allServer = HostInfo.objects.all() AllCount = allServer.count() ret['AllCount'] = AllCount PageObj = Page(AllCount, page, 6) allServerObj = allServer[PageObj.begin:PageObj.end] pageurl = 'index' pageinfo = page_div(page, PageObj.all_page_count, pageurl) ret['PageInfo'] = pageinfo ret['allServerObj'] = allServerObj UserInfoObj = UserInfo.objects.get( username=request.session.get('username', None)) ret['UserInfoObj'] = UserInfoObj return render_to_response('index.html', ret, context_instance=RequestContext(request)) else: return HttpResponse("this is a web page , please use metod GET")