def heartbeats(request): if not request.user.is_authenticated: return HttpResponseRedirect('/login') if request.method == 'POST': is_search = False if request.POST.has_key('search'): is_search = True words = request.POST['search'] search_title = BlogPost.objects.filter(title__contains=words) search_body = BlogPost.objects.filter(body__contains=words) posts = search_body | search_title posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render(request, 'heartbeats.html', { 'posts': posts, 'is_search': is_search, 'word': words }) if request.POST.has_key('body'): bp = BlogPost() bp.title = request.POST['title'] bp.body = request.POST['body'] bp.author = request.user.username bp.timestamp = datetime.now() bp.save() posts = BlogPost.objects.all() posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render( request, 'heartbeats.html', { 'posts': posts, 'is_search': is_search, 'relation_people': relation_people }) else: posts = BlogPost.objects.all() posts = list(posts) relation_people = [] for post in posts: b = User.objects.get(username=post.author) a = Profile.objects.get(user=b) relation_people.extend([a]) posts = zip(posts, relation_people) return render(request, 'heartbeats.html', { 'posts': posts, 'relation_people': relation_people })
def archive(request): posts_obj = BlogPost() posts_obj.title = "HTML leture" posts_obj.content = "format of HTML" posts_obj.timestamp = datetime.now() posts_obj.save() posts = BlogPost.objects.all().order_by("-timestamp") # 1st way # t2 = loader.get_template('blog/archive.html') # t = t2.render({'posts': posts}) # return HttpResponse(t) # 2nd way return render_to_response('blog/archive.html', {'posts': posts})