Django 搭建CMDB系统完整[6](机房搜索、分页)

vi cmdbapp/views.py

-- coding: utf-8 --

from future import unicode_literals

from django.shortcuts import render,render_to_response
from django.core.paginator import Paginator,InvalidPage,EmptyPage
from cmdbapp.models import *
from django.http import HttpResponse

Create your views here.

def main_page(request):
return render_to_response('main_page.html', {'user':request.user })
def base(request):
return render_to_response('base.html')

def search_machinaroom(request):
error = False
each_page = 10
mname=request.GET.get('mname','0')
if mname=='0':
record_list = Machinaroom.objects.all()
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_machinaroom.html',{'machinaroomlist':contacts})
else:
record_list = Machinaroom.objects.filter(name=mname)
paginator = Paginator(record_list,each_page)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('search_machinaroom.html',{'machinaroomlist':contacts})

vi templates/search_machinaroom.html

{% extends 'base.html' %}

{% block title %}












搜索

  机房名字  









{% endblock %}

{% block content %}












{% for mr in machinaroomlist.object_list %}








{% endfor %}


机房名字 机房位置 VPN链接 备注 操作
{{ mr.name }} {{ mr.location }} {{ mr.vpnurl }} {{ mr.memo }}
编辑
删除




{% endblock %}

vi cmdb/urls.py

from django.conf.urls import url
from django.contrib import admin
from cmdbapp import views
from django.contrib.auth.views import *
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import staticfiles
from django.views.static import serve

urlpatterns = [
url(r'^static/(?P.*)',views.main_page,name='main_page'),
url(r'^base',views.base,name='base'),
url(r'^search_machinaroom.html', login),
]

你可能感兴趣的:(Django 搭建CMDB系统完整[6](机房搜索、分页))