刚刚学习django,走了一下午的坑,拿出来分享一下,各位大神有什么更好的“条件查询加分页”,望指教
1.view层
#房间信息给条件 def select_room(request): return render(request, 'select_room_condition.html') #查询房间信息 def select_room2(request,pagenum): condition = request.GET.get("condition") mess = request.GET.get("mess") request.session["condition"]=condition request.session["mess"] = mess print(condition+"-----------------------------------------") if condition=="buildId_id": try: rooms=RPMS_room.objects.filter(buildId_id=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomName": try: rooms=RPMS_room.objects.filter(roomName=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomDate": try: rooms=RPMS_room.objects.filter(roomDate=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomType": try: rooms=RPMS_room.objects.filter(roomType=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomUse": try: rooms=RPMS_room.objects.filter(roomUse=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomArea": try: rooms=RPMS_room.objects.filter(roomArea=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomPercent": try: rooms=RPMS_room.objects.filter(roomPercent=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) if condition=="roomDelete": try: rooms=RPMS_room.objects.filter(roomDelete=mess) pagi = Paginator(rooms, 3) page = pagi.page(pagenum) return render(request, 'select_room.html',{'rooms':page}) except: return render(request, 'select_room_condition.html',{'flag':"信息输入有误"}) return render(request, 'select_room_condition.html', {'flag': "信息输入有误"})
2.url层
urlpatterns = [ url(r'^select_room/$', views.select_room), url(r'^select_room2/(\d)/', views.select_room2), ]
3.给条件的html
{% load static from staticfiles%} <style> #b{ position: relative; background-color: #68C3BE; width: 100%; height: 515px; } [name="condition"]{ position: absolute; top: 70px; left: 380px; width: 150px; height: 40px; text-align: center; font-size: 15px; } [name="mess"]{ position: absolute; top: 70px; left: 560px; width: 200px; height: 38px; font-size: 20px; } [type="submit"]{ position: absolute; top: 70px; left: 800px; width: 100px; height: 40px; background-color: springgreen; border: 1px solid springgreen; } #b1{ position: absolute; top: 80px; left: 180px; width: 150px; height: 50px; font-size: 20px; } #b2{ height: 45px; width: 380px; position: absolute; font-size: 20px; color: red; left: 380px; top: 120px; } </style> <div id="b"> <form action="/RPMSapp/select_room2/1/" method="get"> <span id="b1">请输入查询条件:</span> <select name="condition"> <option >--请选择条件--</option> <option value="buildId_id">楼栋ID号</option> <option value="roomName">房间名</option> <option value="roomDate">入住时间</option> <option value="roomType">房型</option> <option value="roomUse">用途</option> <option value="roomArea">建筑面积</option> <option value="roomPercent">得房率</option> <option value="roomDelete">删除状态码</option> </select>   <input type="text" placeholder="请输入信息" name="mess"> <input type="submit" value="查询"> <span id="b2">{{flag}}</span> </form> </div>
4.查询信息显示的html
{% load static from staticfiles%} <style> #b{ position: relative; background-color: #68C3BE; width: 100%; height: 515px; } #b1{ background-color: white; position: absolute; left: 210px; } td a{ color: red; } </style> <div id="b"> <table border="1px" id="b1" cellpadding="0px" cellspacing="0px"> <tr height="80px"> <th colspan="9" style="font-size: 30px;">本小区楼栋信息</th> </tr> <tr height="50px"> <td width="100px" style="text-align: center">房间ID号</td> <td width="100px" style="text-align: center">楼栋ID号</td> <td width="100px" style="text-align: center">房间名</td> <td width="100px" style="text-align: center">入住时间</td> <td width="100px" style="text-align: center">房型</td> <td width="100px" style="text-align: center">用途</td> <td width="100px" style="text-align: center">建筑面积</td> <td width="100px" style="text-align: center">得房率</td> <td width="100px" style="text-align: center">删除状态码</td> </tr> {% for room in rooms %} <tr height="50px"> <td width="100px" style="text-align: center;"> {{room.roomId}} </td> <td width="100px" style="text-align: center;"> {{room.buildId_id}} </td> <td width="100px" style="text-align: center;"> {{room.roomName}} </td> <td width="100px" style="text-align: center;"> {{room.roomDate}} </td> <td width="100px" style="text-align: center;"> {{room.roomType}} </td> <td width="100px" style="text-align: center;"> {{room.roomUse}} </td> <td width="100px" style="text-align: center;"> {{room.roomArea}} </td> <td width="100px" style="text-align: center;"> {{room.roomPercent}} </td> <td width="100px" style="text-align: center;"> {{room.roomDelete}} </td> </tr> {% endfor %} <tr height="50px"> <td colspan="3" style="text-align: center;"> {% if rooms.has_previous %} <a href="/RPMSapp/select_room2/{{rooms.previous_page_number}}/?condition={{request.session.condition}}&mess={{request.session.mess}}">上一页</a> {% else %} <span>上一页</span> {% endif %} </td> <td colspan="3" style="text-align: center;"> {% for i in rooms.paginator.page_range %} {% if i == rooms.number %} <span>{{i}}</span> {% else %} <a href="/RPMSapp/select_room2/{{i}}/?condition={{request.session.condition}}&mess={{request.session.mess}}">{{i}}</a> {% endif %} {% endfor %} </td> <td colspan="3" style="text-align: center;"> {% if rooms.has_next %} <a href="/RPMSapp/select_room2/{{rooms.next_page_number}}/?condition={{request.session.condition}}&mess={{request.session.mess}}">下一页</a> {% else %} <span>下一页</span> {% endif %} </td> </tr> </table> </div> 注意: 第4部分,上一页下一页的a标签再拼接时,千万不要用"/"结尾,否则出错