def feedback(request): if request.method == "POST": form = FeedbackForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] phone = form.cleaned_data["phone"] email = form.cleaned_data["email"] content = form.cleaned_data["content"] m = Feedback(name=name, phone=phone, email=email, content=content) m.save() return HttpResponseRedirect("/feedback/") else: form = FeedbackForm() messages = Feedback.objects.all() paginator = Paginator(messages, 10) # 这里和下面是分页用 page = request.GET.get("page") # 获取页码 try: contacts = paginator.page(page) # 获取某页对应的记录 except PageNotAnInteger: # 如果页码不是整数 contacts = paginator.page(1) # 去第一页的记录 except EmptyPage: # 如果页码太大没有记录 contacts = paginator.page(paginator.num_pages) # 取最后一页的记录 return render_to_response( "feedback.html", RequestContext(request, {"form": form, "messages": messages, "contacts": contacts}) )
def card(request): values = Card.objects.all() paginator = Paginator(values, 2) # 这里和下面是分页用 page = request.GET.get("page") # 获取页码 try: contacts = paginator.page(page) # 获取某页对应的记录 except PageNotAnInteger: # 如果页码不是整数 contacts = paginator.page(1) # 去第一页的记录 except EmptyPage: # 如果页码太大没有记录 contacts = paginator.page(paginator.num_pages) # 取最后一页的记录 return render_to_response("card.html", {"contacts": contacts})