def home(request, username, *args): user_obj = get_object_or_404(models.UserInfo, username=username) blog_obj = user_obj.blog # 用户对应的博客 article_list = models.Article.objects.filter(user=user_obj) # 获取该用户的文章列表 if args: if args[0] == 'category': article_list = article_list.filter(category__title=args[1]) elif args[0] == 'tag': article_list = article_list.filter(tags__title=args[1]) else: try: y, m = args[1].split('-') article_list = article_list.filter(create_time__year=y, create_time__month=m) except Exception as e: article_list = [] page_num = request.GET.get('page', '1') all_data_amount = article_list.count() url_prefix = '{}'.format(request.path[1:-1]) paging = MyPage(page_num, all_data_amount, url_prefix=url_prefix, per_page_data=10, page_show_tags=9) data = article_list[paging.start:paging.end] page_html = paging.ret_html() return render(request, 'home.html', { 'article_list': data, 'page_html': page_html, 'user_obj': user_obj, 'blog_obj': blog_obj })
def get(self, request): article_list = models.Article.objects.all() # 分页 data_amount = article_list.count() # 文章总数量 page_num = request.GET.get('page', 1) # 通过url 的get请求获取到当前页面 page_obj = MyPage(page_num, data_amount, per_page_data=2, url_prefix=request.path_info[1:-1]) # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] page_html = page_obj.ret_html() return render(request, 'index_new.html', {"article_list": data, "page_html": page_html})
def get(self, request): article_list = models.Article.objects.all() # 分页 data_amount = article_list.count() page_num = request.GET.get('page', 1) page_obj = MyPage(page_num, data_amount, request.path_info, per_page=2) # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] page_html = page_obj.page_html() return render(request, 'index.html', {'article_list': data, 'page_html': page_html})
def get(self, request): article_list = models.Article.objects.all() # 分页 data_amount = article_list.count() page_num = request.GET.get("page", 1) page_obj = MyPage(page_num, data_amount, per_page_data=5, url_prefix='index') # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] page_html = page_obj.ret_html() return render(request, "index.html", {"article_list": data, "page_html": page_html})
def get(self, request): article_list = models.Article.objects.all() # d = {"name": "lvyang", "items": "睡觉"} # d.items() # obj = models.Article.objects.first() # obj.articleupdown_set.all().count() # 分页 data_amount = article_list.count() page_num = request.GET.get("page", 1) page_obj = MyPage(page_num, data_amount, per_page_data=1, url_prefix='index') # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] page_html = page_obj.ret_html() return render(request, "index.html", {"article_list": data, "page_html": page_html})
def get(self, request): article_list = models.Article.objects.all() """ def __init__(self, page_num, all_data_amount, url_prefix, per_page_data=10, page_show_tags=9): :param page_num: 当前页码 :param all_data_amount: 总的数据量 :param url_prefix: 页码a标签的url前缀 :param per_page_data: 每页显示多少条数据 :param page_show_tags: 页面上显示多少个页码 """ page_num = request.GET.get('page', '1') all_data_amount = article_list.count() paging = MyPage(page_num, all_data_amount, url_prefix='index', per_page_data=10, page_show_tags=9) data = article_list[paging.start:paging.end] page_html = paging.ret_html() print(request.user) return render(request, 'index.html', {'article_list': data, 'page_html': page_html})
def get(self, request, username): # 从数据库UserInfo表中找到此用户 user_objs_list = models.UserInfo.objects.filter(username=username) print('user_objs_list---->', user_objs_list) user_obj = user_objs_list.first() article_category = user_obj.blog.tag_set.all() print('article_category---->', article_category) article_tag = user_obj.blog.tag_set.all() print('article_tag---->', article_tag) article_obj = user_obj.article_set.all() print('article_obj--->', article_obj) # 分页 data_amount = article_obj.count() # 文章总数量 page_num = request.GET.get('page', 1) # 通过url 的get请求获取到当前页面 page_obj = MyPage(page_num, data_amount, per_page_data=1, url_prefix='blog') # 按照分页的设置对总数据进行切片 data = article_obj[page_obj.start:page_obj.end] page_html = page_obj.ret_html() return render(request, 'blog.html', {"user_obj": user_obj, "data": data, "page_html": page_html, })
def get(self, request): print(request.user) print(type(request.user)) # print(dir(request.user)) # 【查看此对象都有哪些方法属性】 print(request.user.username, type(request.user.username)) articles = models.Article.objects.all() # return render(request,'index.html',{'articles':articles}) # 【分页】 data_amout = articles.count() page_num = request.GET.get('page', 1) page_obj = MyPage(page_num, data_amout, per_page_data=2, url_prefix='index') # 按照分页的设置对总数居进行切片 data = articles[page_obj.start:page_obj.end] page_html = page_obj.ret_html() return render(request, 'index.html', { 'articles': data, 'page_html': page_html })
def blog_new(request, username, *args): user_obj = models.UserInfo.objects.filter(username=username).first() blog = user_obj.blog article_list = models.Article.objects.filter(user=user_obj) # 左侧侧边栏点击文章分类、文章标签、日期归档跳转到各分类中的文章页面 # 判断args 是否为空,若是空,则说明没有点击左侧侧边栏上的各分类;若不为空,则说明进入各分类中的文章页面 if args: if args[0] == 'category': # 表示按照文章分类查询 article_list = article_list.filter(category__title=args[1]) elif args[0] == 'tag': # 表示按照文章的标签查询 article_list = article_list.filter(tags__title=args[1]) else: # 表示按照文章的日期归档查询(注意:在settings.py将时区改为False) # 先将args[1]的值切割,注意用户可能在-左右乱写一通,故需要异常处理 try: year, month = args[1].split('-') article_list = article_list.filter(create_time__year=year, create_time__month=month) except Exception as e: article_list = [] # 分页 data_amount = article_list.all().count() # 文章总数量 page_num = request.GET.get('page', 1) # 通过url 的get请求获取到当前页面 page_obj = MyPage(page_num, data_amount, per_page_data=2, url_prefix=request.path_info[1:-1]) page_html = page_obj.ret_html() # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] return render(request, 'blog_new.html', {"username": username, "blog": blog, "article_list": data, "page_html": page_html, })
def home(request,username,*args): user_obj = models.UserInfo.objects.filter(username=username).first() if not user_obj: return HttpResponse('404...') blog = user_obj.blog category_list = models.Category.objects.filter(blog=blog) tag_list = models.Tag.objects.filter(blog=blog) archive_list = models.Article.objects.filter(user=user_obj).extra( select={'y_m':"DATE_FORMAT(create_time, '%%Y-%%m')"} ).values('y_m').annotate(c=Count('id')).values('y_m','c') article_list = models.Article.objects.filter(user=user_obj) user_obj = models.UserInfo.objects.filter(username=username).first() data_amount = article_list.count() page_num = request.GET.get("page", 1) page_obj = MyPage(page_num, data_amount, per_page_data=10, url_prefix='index') # 按照分页的设置对总数据进行切片 data = article_list[page_obj.start:page_obj.end] page_html = page_obj.ret_html() if args: if args[0] == 'category': article_list = article_list.filter(category__title=args[1]) elif args[0] == 'tag': article_list = article_list.filter(tags__title=args[1]) else: try: year, month = args[1].split("-") article_list = article_list.filter(create_time__year=year,create_time__month=month) except Exception as e: article_list = [] return render(request,'home.html',{'blog':blog,'article_list':article_list,'category_list':category_list,'tag_list':tag_list,'archive_list':archive_list,'user':user_obj,'username':username,'page_html':page_html,})