def blog_with_type(request, blog_type_id): # if request.GET.get("today") == '2019': # 从GET中获取到相应参数的值 # return redirect("http://www.baidu.com") blog_type = get_object_or_404(BlogType, id=blog_type_id) blogs = Blog.objects.filter(blog_type=blog_type) # 分页器 context = page(request, blogs) context["all_num"] = blogs.count() context["type"] = blog_type hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) context["hot_blogs"] = hot_blogs context['register_form'] = RegisterForm() context['login_form'] = LoginForm() blog_content_type = ContentType.objects.get_for_model(Blog) context['get_today_hot_blog'] = get_today_hot( content_type=blog_content_type) context['get_yesterday_hot_blog'] = get_yesterday_hot( content_type=blog_content_type) context['get_week_hot_blog'] = get_week_hot_blog() if request.user.is_authenticated: context['mesg_num'] = Notification.objects.filter( recipient=request.user).unread().count() return render(request, 'blog_with_type.html', context)
def blog(request): search = request.GET.get('search') if search: #使用Q对象进行联合搜索 blogs_list = Blog.objects.filter( Q(title__icontains=search) | Q(content__icontains=search)) else: search == '' blogs_list = Blog.objects.all() context = page(request, blogs_list) context['search_num'] = blogs_list.count() context['search'] = search hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) context["hot_blogs"] = hot_blogs blog_content_type = ContentType.objects.get_for_model(Blog) week_readnum_list(context) get_week_hot_blogs = cache.get('get_week_hot_blogs') if get_week_hot_blogs is None: get_week_hot_blogs = get_week_hot_blog() cache.set('get_week_hot_blogs', get_week_hot_blogs, 3600) week_yesterday_today_hotbloglist(context) get_form_all(context) context['blogs_dates'] = Blog.objects.dates("created_time", 'month', order='DESC') if request.user.is_authenticated: context['mesg_num'] = Notification.objects.filter( recipient=request.user).unread().count() return render(request, 'blog2.html', context)
def blog_detail(request, blog_id): context = {} blog = get_object_or_404(Blog, id=blog_id) """ 阅读加1,并且返回cookies """ read_cookies_key = read_tool(request, blog) #将blog模型传给content_type:ct=blog # 获取该blog的type the_blog_type = blog.blog_type # 获取该type的blog these_blogs = Blog.objects.filter(blog_type=the_blog_type) context["all_num"] = these_blogs.count() # left context['blog'] = blog # 获取上一篇(该类型下) context['first_blog'] = Blog.objects.filter( blog_type=the_blog_type, created_time__lt=blog.created_time).first() # 获取下一篇 context['last_blog'] = Blog.objects.filter( blog_type=the_blog_type, created_time__gt=blog.created_time).last() # right blogs_type = BlogType.objects.all() context["blogs_type"] = blogs_type # 日期归档 context['blogs_dates'] = Blog.objects.dates("created_time", 'month', order='DESC') # 统计数量 context["blogs_type"] = BlogType.objects.annotate( blogs_count=Count('blog')) # 返回评论列表 content_type_blog = ContentType.objects.get_for_model(blog) hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) # print(hot_blogs) context["hot_blogs"] = hot_blogs context['login_form'] = LoginForm() context['register_form'] = RegisterForm() blog_content_type = ContentType.objects.get_for_model(Blog) context['get_today_hot_blog'] = get_today_hot( content_type=blog_content_type) context['get_yesterday_hot_blog'] = get_yesterday_hot( content_type=blog_content_type) context['get_week_hot_blog'] = get_week_hot_blog() if request.user.is_authenticated: context['mesg_num'] = Notification.objects.filter( recipient=request.user).unread().count() response = render(request, 'blog_detail.html', context) # 浏览器关闭失效 response.set_cookie(read_cookies_key, 'true') #阅读标记 return response
def user_info(request): context = {} if request.user.is_authenticated: collect_list = CollectRecord.objects.filter(user=request.user) #使用分页器进行处理 context = page(request, collect_list) context['collect_list'] = collect_list if request.method == 'POST': # 将提交的数据赋值给表单实例 article_post_form = ArticlePostForm(request.POST, request.FILES) if article_post_form.is_valid(): # 保存数据但是暂时不提交到数据库中 post_article = article_post_form.save(commit=False) # 指定文章作者为请求方 post_article.author = request.user # 然后保存到数据库 post_article.save() context['post_tip'] = '发送成功' return redirect('blog_detail', post_article.pk) else: context['message'] = article_post_form.errors return render(request, 'error.html', context) else: context['message'] = '请确认是否登录状态' return render(request, 'error.html', context) if request.user.is_superuser: personal_blog_list = Blog.objects.filter(author=request.user) context['personal_blog_list'] = personal_blog_list context['article_form'] = ArticlePostForm() context['login_form'] = LoginForm() context['register_form'] = RegisterForm() context['change_password_form'] = ChangePasswordForm() context['change_nickname_form'] = ChangeNicknameForm() context['bing_email_form'] = BindEmailForm() user = request.user context['all_msg'] = Notification.objects.filter(recipient=user).unread() context['num'] = Notification.objects.filter( recipient=user).unread().count() hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) context["hot_blogs"] = hot_blogs return render(request, 'user_info.html', context)
def blog_with_date(request, year, month): blogs = Blog.objects.filter(created_time__year=year, created_time__month=month) # 分页器 context = page(request, blogs) context['month'] = month context['year'] = year context["all_num"] = blogs.count() hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) context["hot_blogs"] = hot_blogs get_form_all(context) week_yesterday_today_hotbloglist(context) week_readnum_list(context) if request.user.is_authenticated: context['mesg_num'] = Notification.objects.filter( recipient=request.user).unread().count() return render(request, 'blog_with_date.html', context)
def blog(request): search = request.GET.get('search') if search: #使用Q对象进行联合搜索 blogs_list = Blog.objects.filter( Q(title__icontains=search) | Q(content__icontains=search)) else: search == '' blogs_list = Blog.objects.all() context = page(request, blogs_list) context['search_num'] = blogs_list.count() context['search'] = search hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) # print(hot_blogs) context["hot_blogs"] = hot_blogs blog_content_type = ContentType.objects.get_for_model(Blog) read_num_sum, each_date_day_list = get_seven_days_read_date( content_type=blog_content_type) get_week_hot_blogs = cache.get('get_week_hot_blogs') if get_week_hot_blogs is None: get_week_hot_blogs = get_week_hot_blog() cache.set('get_week_hot_blogs', get_week_hot_blogs, 3600) context['read_num_sum_list'] = read_num_sum context['each_date_day_list'] = each_date_day_list context['get_today_hot_blog'] = get_today_hot( content_type=blog_content_type) context['get_yesterday_hot_blog'] = get_yesterday_hot( content_type=blog_content_type) context['get_week_hot_blog'] = get_week_hot_blog() context['register_form'] = RegisterForm() context['login_form'] = LoginForm() if request.user.is_authenticated: context['mesg_num'] = Notification.objects.filter( recipient=request.user).unread().count() return render(request, 'blog2.html', context)