def index(request, tag_name=None, title_blog="Latest posts"): """Main entry point for the blog. Show all post using pagination Keyword arguments: tag_name -- If tag_name is not None then showing post by tag """ title = ".:: BSNUX - Arturo Fernandez - Blog ::." # To calculate max. links for pagination PAGES_LINK_COUNT = 5 # Number of items per page ITEMS_PER_PAGE = 4 # Activating tab blo_active = True # Template template = 'blog/index.html' # If tag was sended then getting all posts by tag if tag_name: latest_post_list_all = Tag.objects.get(name=tag_name).post_set.all().order_by('-created_at') else: latest_post_list_all = Post.objects.all().order_by('-created_at') paginator = Paginator(latest_post_list_all, ITEMS_PER_PAGE) # Manage possible error if a wrong page number is received try: page = int(request.GET.get('p', '1')) except ValueError: page = 1 try: latest_post_list = paginator.page(page) except (EmptyPage, InvalidPage): latest_post_list = paginator.page(paginator.num_pages) start_index = page - PAGES_LINK_COUNT end_index = page + PAGES_LINK_COUNT if start_index <= 0: start_index = 1 end_index = (PAGES_LINK_COUNT*2)+1 if end_index > paginator.num_pages: end_index = paginator.num_pages arr_pages = range(start_index, end_index) tag = Tag() # Variables for the template and rendering dict_vars = { 'latest_post_list': latest_post_list, 'arr_pages': arr_pages, 'tag_cloud': tag.get_tag_cloud(), 'title': title, 'blo_active': blo_active, 'title_head': title_blog, } return render_to_response(template, dict_vars)
def post(request, post_id, permalink): """Showing a specific post Keyword arguments: post_id -- ID for this post permalink -- permalink for this post """ title = ".:: BSNUX - Arturo Fernandez - Blog. " template = 'blog/post.html' blo_active = True post = Post.objects.get(id=post_id) title += post.title + '::.' tag = Tag() dict_vars = { 'post': post, 'title': title, 'blo_active': blo_active, 'tag_cloud': tag.get_tag_cloud(), } return render_to_response(template, dict_vars)