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 blog_publish(request): """ 保存博客 :param request: :return: """ if request.method == "POST": data = request.POST content = data.get("content") # 保存文章数据到数据库 id = data.get('id', 0) if id != 0: blog = BlogPost.objects.filter(author_id=request.user.id, id=id) else: blog = BlogPost.objects.filter(author_id=request.user.id, statu='正在编辑') if blog.exists(): blog = blog.first() blog.tag.clear() else: blog = BlogPost() blog.content = content blog.statu = data.get("statu") blog.author = User.objects.get(id=request.user.id) blog.category_id = data.get("category") blog.blog_type = data.get("type") blog.title = data.get('title') tag_list = [] blog.modify_time = now() blog.save() if data.get("tag_add"): tag_add = blog_tag(data.get("tag_add")) # print(data.get("tag_add")) tag_list = tag_add if data.get('tag'): for tag in data.get('tag').split(','): tag_list.append(tag) # print(tag_list) if len(tag_list) > 0: tag_list = list(set(tag_list)) if '' in tag_list: tag_list.remove('') blog.tag.add(*tag_list) context = {"flag": True, "message": "博文发布成功!", 'id': blog.id} return JsonResponse(context) else: context = {"categorys": blog_category(), 'tags': blog_tag()} return JsonResponse(context)
def publish(request, id): context = {} user = request.user if not user.is_authenticated: return redirect("login") notes = get_object_or_404(Notes, pk=id) if notes.author != user: return HttpResponse("You are not the author of that post.") blog_post = BlogPost() blog_post.title = notes.title blog_post.body = notes.body blog_post.author = request.user blog_post.save() return redirect("/")