def index(request): posts = Post.objects.order_by('-publish_date').filter( status="p") # showing only published posts post_per_page = get_post_per_page(request) p = Paginator(posts, post_per_page) total_pages = p.num_pages # or last page # pagination if request.GET.get('page'): try: page = int(request.GET.get('page')) except: page = 1 # if direct homepage else: page = 1 try: latest_posts = p.page(page) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. latest_posts = p.page(total_pages) page = total_pages except PageNotAnInteger: # If page is not an integer, deliver first page. latest_posts = p.page(1) page = 1 if request.user.is_authenticated(): """ if logged in user liked post will be marked in homepage """ user = UserExtended.objects.get(user=request.user) for p in latest_posts: try: p.have_like = "1" if UserPostRelation.objects.get( user=user, post=p) else "0" except: p.have_like = "0" pg_iter = long_pagination( current_page=page, total_pages=total_pages, showing=get_page_number_in_pagination( request), # page number showing in pagination is_not_mobile=not request.is_mobile) popular_posts = Post.objects.order_by('-likes')[:5] all_cats = Category.objects.filter(post__likes__isnull=False).annotate( like_count=Sum('post__likes')).order_by('-like_count') ctx = { # context "posts": latest_posts, "popular_posts": popular_posts, "popular_cats": all_cats[:5], "all_cats": all_cats, "page_iter": pg_iter, "current_page": page, } return render(request, 'main_app/index.html', ctx)
def each_category(request, slug): try: category = Category.objects.get(slug=slug) posts_by_category = Post.objects.filter( categorize__category=category).order_by('-publish_date') post_per_page = get_post_per_page(request) p = Paginator(posts_by_category, post_per_page) total_pages = p.num_pages # or last page # pagination if request.GET.get('page'): try: page = int(request.GET.get('page')) except: page = 1 # if direct homepage else: page = 1 try: categorized_post = p.page(page) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. categorized_post = p.page(total_pages) page = total_pages except PageNotAnInteger: # If page is not an integer, deliver first page. categorized_post = p.page(1) page = 1 if request.user.is_authenticated(): """ if logged in user liked post will be marked in homepage """ user = UserExtended.objects.get(user=request.user) for p in categorized_post: try: p.have_like = "1" if UserPostRelation.objects.get( user=user, post=p) else "0" except: p.have_like = "0" pg_iter = long_pagination( current_page=page, total_pages=total_pages, showing=get_page_number_in_pagination( request), # page number showing in pagination is_not_mobile=not request.is_mobile) except Category.DoesNotExist: raise Http404( "আপনার এহেন বোকামির জন্য আমরা শোক প্রকাশ করছি।কারণ, এই নামের কিছুই খুঁজে পাওয়া যায় নি!" ) popular_posts = Post.objects.order_by('-likes')[:5] popular_cats = Category.objects.filter(post__likes__isnull=False).annotate( like_count=Sum('post__likes')).order_by('-like_count')[:5] ctx = { "is_category_template": True, "posts": categorized_post, "popular_posts": popular_posts, "popular_cats": popular_cats, "page_iter": pg_iter, "current_page": page, } return render(request, 'main_app/index.html', ctx)