def index(request, *args, **kwargs): """ 首頁主站 """ # 文章內建分類列表 article_type_list = models.Article.type_choices # 判斷分類和url if kwargs: article_type_id = int(kwargs['article_type_id']) base_url = reverse('index', kwargs=kwargs) else: article_type_id = None base_url = "/" # 分頁 data_count = models.Article.objects.filter(**kwargs).count() # 文章數 current_page = request.GET.get('p') print(current_page) page_obj = Pagination(data_count, current_page, perPageItemNum=4) page_str = page_obj.page_str(base_url) # 篩選文章(照nid倒序排列),並根據頁數不同而改變文章 article_list = models.Article.objects.filter( **kwargs).order_by('-nid')[page_obj.start():page_obj.end()] blog_list = models.Blog.objects.all()[:5] return render(request, "index.html", locals())
def tag(request): # -----------------要有blog才可使用----------------- blog_id = request.session['user_info']['blog__nid'] if blog_id is None: return redirect("/backend/index.html") # -------------------------------------------------- blog_id = request.session["user_info"]["blog__nid"] # 分頁 data_count = models.Tag.objects.filter(blog_id=blog_id).count() current_page = request.GET.get("p") page_obj = Pagination(data_count, current_page, perPageItemNum=7) page_str = page_obj.page_str("/backend/tag.html") result = models.Tag.objects.filter( blog_id=blog_id)[page_obj.start():page_obj.end()] return render(request, "backend_tag.html", locals())
def article(request, *args, **kwargs): condition = {} # -----------------要有blog才可使用----------------- blog_id = request.session['user_info']['blog__nid'] if blog_id is None: return redirect("/backend/index.html") # -------------------------------------------------- if kwargs: base_url = reverse("article", kwargs=kwargs) for k, v in kwargs.items(): temp = int(v) kwargs[k] = temp if temp: # 不等於0就加入 condition[k] = temp else: base_url = "/backend/article-0-0.html" # print(base_url) condition["blog_id"] = blog_id article_type_list = map(lambda item: { 'nid': item[0], 'title': item[1] }, models.Article.type_choices) category_list = models.Category.objects.filter(blog_id=blog_id) # 分頁 data_count = models.Article.objects.filter(**condition).count() current_page = request.GET.get('p') page_obj = Pagination(data_count, current_page, perPageItemNum=5) page_str = page_obj.page_str(base_url) result = models.Article.objects.filter( **condition).order_by("-nid")[page_obj.start():page_obj.end()] return render(request, "backend_article.html", locals())