Facebook
From Szpagat, 7 Months ago, written in Python.
This paste is a reply to Django-pages from Szpagat - view diff
Embed
Download Paste or View Raw
Hits: 542
  1. "pagination.html"
  2. {% load bootstrap4 %}
  3. <div class="container">
  4.     <nav aria-label="Page navigation example">
  5.         <ul class="pagination">
  6.             {% if page.has_previous %}
  7.             <li class="page-item"><a class="page-link" href="?page={{ page.previous_page_number }}">Previous</a>
  8.             </li>
  9.             {% endif %}
  10.             {% if page.has_next %}
  11.             <li class="page-item"><a class="page-link" href="?page={{ page.next_page_number }}">Next</a>
  12.             </li>
  13.             {% endif %}
  14.         </ul>
  15.     </nav>
  16. </div>
  17.  
  18. "list.html"
  19. {% extends "blog/base.html" %}
  20.  
  21. {% block title %} Szpagat {% endblock %}
  22.  
  23. {% block content %}
  24.     {% for post in posts %}
  25.       <h2>
  26.       <a href="{{ post.get_absolute_url }}">
  27.         {{ post.title}}
  28.       </a>
  29.       </h2>
  30.       <p class="date">
  31.       Opublikowany {{ post.publish }} przez {{ post.author }}
  32.       </p>
  33.       {{ post.body|truncatewords:30|linebreaks }}
  34.     {% endfor %}  
  35. {% include "pagination.html" with page=posts %}
  36. {% endblock %}
  37.  
  38. kod:
  39.   def post_list(request):
  40.     object_list = Post.published.all()
  41.     paginator = Paginator(object_list, 3)
  42.     page = request.GET.get('page')
  43.     try:
  44.         posts = paginator.page(page)
  45.     except PageNotAnInteger:
  46.        
  47.         posts = paginator.page(1)
  48.     except EmptyPage:
  49.        
  50.         posts = paginator.page(paginator.num_pages)
  51.     return render(request,
  52.                     'blog/post/list.html',
  53.                     {'page': page,
  54.                     'posts': posts})