Example #1
0
def home(request, articleType,url='/'):
        request.session.set_expiry(0)
        username = request.session.get('username', False)
        print(username)
        if not username:
            pass#username = '******'
        if request.method == "POST":
            comment=Comment()
            comment.username=request.session.get('username', False)
            if url == '/':
                blog_list = BlogsPost.objects.filter(artcileType=articleType)
                thisBlog = BlogsPost.objects.get(title=blog_list[0].title)
                comment.title=thisBlog.title
                comment.time=datetime.datetime.now() 
            else:
                thisBlog = BlogsPost.objects.get(title=url)
                thisBlog.commentNum=thisBlog.commentNum+1;
                thisBlog.save();
                comment.title=thisBlog.title
                comment.time=datetime.datetime.now()
            comment.userimg='/static/img/jslogo.jpg'
            comment.body=request.POST.get('content',False)
            comment.save()
            if url=='/':
                comments=Comment.objects.filter(title=blog_list[0].title)
                return HttpResponseRedirect('/', {'posts': blog_list, 'post': blog_list[0], 'username': username,'comments':comments})
            else:
                blog_list = BlogsPost.objects.filter(artcileType=articleType)
                thisBlog = BlogsPost.objects.get(title=url)
                comments=Comment.objects.filter(title=thisBlog.title)
                return HttpResponseRedirect(url, {'post': thisBlog, 'posts': blog_list, 'username': username,'comments':comments})
        else:
            blog_list = BlogsPost.objects.filter(artcileType=articleType)
            for blog in blog_list:
                blog.url = "/article/" +blog.artcileType + '/'+blog.title
            if url == '/':
                comments=Comment.objects.filter(title=blog_list[0].title)
                #print(blog_list[0].body)
                return render_to_response('home.html', {'posts': blog_list, 'post': blog_list[0], 'username': username,'comments':comments})
            else:
                thisBlog = BlogsPost.objects.get(title=url)
                thisBlog.readNum =thisBlog.readNum+1
                thisBlog.save()
                comments=Comment.objects.filter(title=thisBlog.title)
                return render_to_response('home.html', {'post': thisBlog, 'posts': blog_list, 'username': username,'comments':comments})