def get_page_html(self, request, data_list): page = request.GET.get('page', 1) page_obj = Pagination(page, len(data_list), self.config.get_list_url(), request.GET, per_page_num=5) new_data_list = data_list[page_obj.start:page_obj.end] page_html = page_obj.page_html() return new_data_list, page_html
def get(self,request,bottom_url,**kwargs): # 从url中获取bottom_url参数,根据获取的bottom_url查找对应站点 blog = models.Blog.objects.filter(bottom_url=bottom_url).first() if not blog: return render(request, 'error.html', {'error_info': '没有对应的个人博客!'}) # 然后根据站点查找对应的用户名 user = models.UserInfo.objects.filter(blog=blog).first() # 置顶的博客 sticky_article_list = None # 要展示的博客 article_list = None # 分页查看时网页后缀 page_url = None # 根据**kwargs的值来判断用户进的是哪个页面(分类、时间归档、或者搜索),然后查询相对于的博客列表传递过去 if not kwargs: # 如果kwargs没有接收值,那么说明是进入站点首页 article_list = models.Article.objects.filter(user=user,is_post=True).order_by('-pk') sticky_article_list = article_list.filter(is_stick=1,is_post=True) page_url = bottom_url else: view = kwargs.get('view') condition = kwargs.get('condition') if view == 'category': article_list = models.Article.objects.filter(user=user,category__title=condition,is_post=True) elif view == 'search': article_list = models.Article.objects.filter(Q(title__contains=condition)|Q(articledetail__content__contains=condition)).filter(is_post=True) elif view == 'archive': year,month = condition.split('-') article_list = models.Article.objects.filter(is_post=True,user=user,create_time__year=year,create_time__month=month) page_url = '/{}/articles/{}/{}'.format(bottom_url, view, condition) # 分页展示 page = request.GET.get('page',1) page_obj = Pagination(article_list.count(),page,page_url) if article_list: article_list = article_list[page_obj.start:page_obj.end] page_html = page_obj.page_html() data = { 'blog':blog, 'user':user, 'bottom_url':bottom_url, 'article_list':article_list, 'sticky_article_list':sticky_article_list, 'page_html':page_html, } return render(request,'home.html',data)
def get(self, request): url_prefix = request.path_info print(request.get_full_path()) print(request.GET) qd = request.GET.copy( ) # <QueryDict: {'query': ['了']}> 查找的'了',所以query获取的就是登陆者所查找的内容 # qd._mutable = True # 让QueryDict对象可修改 # print(request.GET.urlencode()) # 把检索参数都编码成URL current_page = request.GET.get('page', 1) if '/my_customer/' in request.path_info: # 获取私户信息,Customer类中查找consultant这个字段属于是当前销售用户的客户 query_set = Customer.objects.filter( consultant=request.user) # 获取此时登录的销售用户的名称 else: # 获取公户的所有信息 query_set = Customer.objects.filter( consultant__isnull=True) # 把consultant这个字段为空的都提取出来 # 根据模糊检索的条件对query_set再做过滤 # 找到name,qq,qq_name字段包含QuerySet_value的那些数据就是搜索的结果 q = self._get_query_q(['name', 'qq', 'qq_name']) query_set = query_set.filter(q) # query_set = Customer.objects.all() # total_count = query_set.count() # SQL语句的效率高 # 生成一个分页实例 page_obj = Pagination(current_page, query_set.count(), url_prefix, qd, per_page=2) print(page_obj.start, page_obj.end) # 取到当前页面的数据 data = query_set[page_obj.start:page_obj.end] # 2.返回之前的页面 # 获取当前请求的带Query参数的URL url = request.get_full_path() # 生成一个空的QueryDict对象 query_params = QueryDict(mutable=True) # 添加一个next键值对 query_params['next'] = url # 利用QueryDict内置的方法编码成URL next_url = query_params.urlencode() # 取到分页的HTML代码 page_html = page_obj.page_html() return render(request, 'customer_list.html', { 'customer_list': data, 'next_url': next_url, 'page_html': page_html })
def site(request, username, **kwargs): user_obj = models.UserInfo.objects.filter(username=username).first() if not user_obj: return render(request, 'error.html') blog = user_obj.blog #获取此用户的文章 article_list = models.Article.objects.filter(blog=blog) if kwargs: condition = kwargs.get('condition') param = kwargs.get('param') if condition == 'category': article_list = article_list.filter(category_id=param) elif condition == 'tag': article_list = article_list.filter(tags=param) else: year, month = param.split('-') article_list = article_list.filter(create_time__year=year, create_time__month=month) #使用分页器 page_obj = Pagination(current_page=request.GET.get('page', 1), all_count=article_list.count()) page_queryset = article_list[page_obj.start:page_obj.end] return render(request, 'site.html', locals())
def home(request): # 将当前网站上的所有的文章全部展示到前端页面 article_list = models.Article.objects.all() page_obj = Pagination(current_page=request.GET.get('page', 1), all_count=article_list.count()) page_queryset = article_list[page_obj.start:page_obj.end] return render(request, 'home.html', locals())
def site(request, username, **kwargs): user_obj = models.User.objects.filter(username=username) if not user_obj: return render(request, '404.html') article_list = models.Article.objects.filter(blog__user__username=username) if kwargs: conditions = kwargs.get('conditions') params = kwargs.get('params') if conditions == 'category': article_list = article_list.filter(category__pk=params) if not article_list: return render(request, '404.html') elif conditions == 'tag': article_list = article_list.filter(tags__pk=params) if not article_list: return render(request, '404.html') elif conditions == 'achieve': year, month = params.split('-') article_list = article_list.filter(create_time__year=year, create_time__month=month) if not article_list: return render(request, '404.html') else: return render(request, '404.html') page_obj = Pagination(current_page=request.GET.get('page'), all_count=article_list.count(), per_page_num=5) page_queryset = article_list[page_obj.start:page_obj.end] return render(request, 'blog/site.html', locals())
def site(request, username): #先校验当前用户名对应的个人站点是否存在 user_obj = models.UserInfo.objects.filter(username=username).first() #用户如果用户不存在则返回一个404页面 if not user_obj: return render(request, 'errors.html') #用户如果存在,拿到个人站点 blog = user_obj.blog #查询个人站点下所有的文章并展示 article_list = models.Article.objects.filter(blog=blog) #获取个人站点下的所有文章列表 current_page = request.GET.get('page', 1) # 获取当前页 all_count = article_list.count() # 获取总数据条数 # 实例化传值生成对象 page_obj = Pagination(current_page=current_page, all_count=all_count) # 直接对总数据进行切片操作(按实例化对象里面的起始页和结束页进行切) page_queryset = article_list[page_obj.start:page_obj.end] #1.查询当前用户所有分类及分类下的文章 category_list = models.Category.objects.filter(blog=blog).annotate( count_num=Count('article__pk')).values('name', 'count_num') """ .annotate聚合函数统计之前先分组 values('name','count_num') 拿到名字跟统计个数 """ # print(category_list)#<QuerySet [{'name': 'jason的分类一', 'count_num': 2}, {'name': 'jason的分类二', 'count_num': 1}, {'name': 'jason的分类三', 'count_num': 1}]> #2.查询当前用户所有的标签及标签下的文章数 tag_list = models.Tag.objects.filter(blog=blog).annotate( count_num=Count('article__pk')).values('name', 'count_num') print(tag_list) #3.按年月统计分组,导入T润肠Month return render(request, 'site.html', locals())
def backend(request): # 获取当前用户所有的文章 article_list = models.Article.objects.filter(blog=request.user.blog) article_obj = Pagination(current_page=request.GET.get('page', 1), all_count=article_list.count()) article_queryset = article_list[article_obj.start:article_obj.end] return render(request, 'backend/backend.html', locals())
def home(request): article_list = models.Article.objects.all() page_obj = Pagination(current_page=request.GET.get('page', 1), all_count=article_list.count()) page_queryset = article_list[page_obj.start:page_obj.end] for article in page_queryset: print(article.blog) return render(request, 'home.html', locals())
def home(request): article_list = models.Article.objects.all() page_obj = Pagination(current_page=request.GET.get('page'), all_count=article_list.count(), per_page_num=5) page_queryset = article_list[page_obj.start:page_obj.end] return render(request, 'blog/home.html', locals())
def serach(request): keyword = request.GET.get('keyword') # 忽略大小写的模糊查询 serach_list = models.Article.objects.filter(title__icontains=keyword) page_obj = Pagination(current_page=request.GET.get('page', 1), all_count=serach_list.count()) page_queryset = serach_list[page_obj.start:page_obj.end] return render(request, 'serach.html', locals())
def home(request): #查询本网站所有的文章数据展示到前端页面,这里可以使用分页器做分页 article_queryset = models.Article.objects.all() #拿到所有文章 current_page = request.GET.get('page', 1) # 获取当前页 all_count = article_queryset.count() # 获取总数据条数 # 实例化传值生成对象 page_obj = Pagination(current_page=current_page, all_count=all_count) # 直接对总数据进行切片操作(按实例化对象里面的起始页和结束页进行切) page_queryset = article_queryset[page_obj.start:page_obj.end] return render(request, 'home.html', locals())
def my_paging(request): from utils.mypage import Pagination # 获取到当前页码 current_page = request.GET.get('page', 1) # 获取所有数据对象 book_queryset = models.Book.objects.all() # 统计书籍总数 all_count = book_queryset.count() # 调用第三方分页插件生成对象 page_obj = Pagination(current_page=current_page, all_count=all_count) # 分页返回数据 page_queryset = book_queryset[page_obj.start:page_obj.end] return render(request, 'app05/my_paging.html', locals())
def backend(request): # 获取当前用户的所有文章 article_list = models.Article.objects.filter(blog=request.user.blog) current_page = request.GET.get("page", 1) all_count = article_list.count() # 1、传值生成对象 page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=10) # 2、对总数据进行切片操作 page_queryset = article_list[page_obj.start:page_obj.end] # 3、将page_queryset传递到前端页面 return render(request, 'backend/backend.html', locals())
def get(self, request): url_prefix = request.path_info qd = request.GET.copy() # <QueryDict: {'query': ['了']}> # qd._mutable = True # 让QueryDict对象可修改 current_page = request.GET.get('page', 1) if request.path_info == reverse('my_customer'): # 获取私户信息 query_set = Customer.objects.filter( consultant=request.user) # request.user-->当前登录的人 else: # 获取所有公户信息 query_set = Customer.objects.filter(consultant__isnull=True) # 根据模糊检索的条件对query_set再做过滤. # 找到name, qq, qq_name字段包含query_value的哪些数据就是搜索的结果 q = self._get_query_q(['name', 'qq', 'qq_name']) query_set = query_set.filter(q) page_obj = Pagination(current_page, query_set.count(), url_prefix, qd, per_page=3) data = query_set[page_obj.start:page_obj.end] # 2.0 返回之前的页面 # 2.1 获取当前请求的带query参数的URL url = request.get_full_path() # 2.2 生成一个空的QueryDict对象 query_params = QueryDict(mutable=True) # 2.3 添加一个next键值对 query_params['next'] = url # 2.4 利用QueryDict内置方法编码成URL next_url = query_params.urlencode() # 在页面上展示出来 return render( request, 'customer_list.html', { 'customer_list': data, 'next_url': next_url, 'page_html': page_obj.page_html() })
def get(self,request,view): user = request.user blog = user.blog bottom_url = blog.bottom_url # 分页展示的页面url page_url = 'manage/{}'.format(view) page = request.GET.get('page', 1) page_obj = None data_list = None # 数据 try: # 如果数据库暂时没有数据,切片会报错 if view == 'article': article_list = models.Article.objects.filter(user=user,is_post=True).order_by('-pk') page_obj = Pagination(article_list.count(), page, page_url) data_list = article_list[page_obj.start:page_obj.end] elif view == 'drafts': drafts_list = models.Article.objects.filter(user=user,is_post=False).order_by('-pk') page_obj = Pagination(drafts_list.count(), page, page_url) data_list = drafts_list[page_obj.start:page_obj.end] elif view == 'category': category_list = models.Category.objects.filter(blog=blog) page_obj = Pagination(category_list.count(), page, page_url,12) data_list = category_list[page_obj.start:page_obj.end] else: link_list = models.FriendlyLink.objects.filter(blog=blog) page_obj = Pagination(link_list.count(), page, page_url) data_list = link_list[page_obj.start:page_obj.end] except AssertionError: data_list = [] # 分页 page_html = page_obj.page_html() data = { 'user':user, 'blog':blog, 'bottom_url':bottom_url, 'page_html':page_html, 'data_list':data_list, } return render(request,'manage/{}-manage.html'.format(view),data)
def category(request): category_list = models.Category.objects.filter(blog=request.user.blog) category_obj = Pagination(current_page=request.GET.get('page', 1), all_count=category_list.count()) category_queryset = category_list[category_obj.start:category_obj.end] return render(request, 'backend/category.html', locals())