def ShowComment(request): if request.method == "POST": comment=request.POST.get('comment') yourname=request.POST.get('name') if yourname=='': yourname='匿名' canshow=request.POST.get('canshow') time=datetime.datetime.now() if canshow=='True': canshow=True else: canshow=False mycomment=BlogComment() mycomment.comments=comment mycomment.canshow=canshow mycomment.name=yourname mycomment.timestamp=time mycomment.save() lengh=len(BlogComment.objects.all()) try: num=request.GET['p'] num=int(num) except: num=1 #我们先来确定这个范围 if num==0: num=1 if lengh<=showlen: start=0 end=showlen else: start=(num-1)*showlen end=num*showlen username = request.COOKIES.get('username') if username == 'god': posts=BlogComment.objects.all().order_by('-id')[start:end] else: posts=BlogComment.objects.filter(canshow__exact=True).order_by('-id')[start:end] IsLogin=checkLogin(request) t = loader.get_template("guestbook.html") c = Context({'posts':posts,'number':num,'pre':num-1,'next':num+1,'IsLogin':IsLogin}) return t.render(c)
def blog_comment_old(request, blog_id): blog_id = int(blog_id) try: blog = Blog.objects.get(id=blog_id) except Blog.DoesNotExist: raise Http404 if request.method == "POST": name = request.POST.get('name', None) comment = request.POST.get('comment', None) has_error = False error_messages = [] if not (name and comment): has_error = True error_messages += ["All fields are required."] if name == "Sadjad": has_error = True error_messages += ["Go away!"] if comment and len(comment) < 10: has_error = True error_messages += ["Your comment is very short!"] if not has_error: blogcomment = BlogComment() blogcomment.blog = blog blogcomment.name = name blogcomment.comment = comment blogcomment.save() return render(request, "blog_comment.html", { 'blog': blog, 'has_error': has_error, 'error_messages': error_messages }) return render(request, "blog_comment.html", { 'blog': blog })