Add pagination support for blog listing
This commit is contained in:
parent
7b3ec3f916
commit
87d7e16b0e
@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% if posts %}
|
{% if posts %}
|
||||||
|
{% include 'paginator.html' with list=posts %}
|
||||||
{% for post in posts %}
|
{% for post in posts %}
|
||||||
{% with title_links=True %}
|
{% with title_links=True %}
|
||||||
{% include 'blog/post.html' %}
|
{% include 'blog/post.html' %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% include 'paginator.html' with list=posts %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>No posts are returned for your query.</p>
|
<p>No posts are returned for your query.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -3,7 +3,9 @@ from blog import views
|
|||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', views.index, name='index'),
|
url(r'^$', views.index, name='index'),
|
||||||
url(r'^feed$', views.feed, name='feed'),
|
url(r'^page/(?P<page>\d+)$', views.indexpage, name='indexpage'),
|
||||||
url(r'^tag/(?P<tag>.*)$', views.taglist, name='taglist'),
|
url(r'^tag/(?P<tag>[^/]+)$', views.taglist, name='taglist'),
|
||||||
|
url(r'^tag/(?P<tag>[^/]+)/page/(?P<page>\d+)$', views.tagpage, name='tagpage'),
|
||||||
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
|
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
|
||||||
|
url(r'^feed$', views.feed, name='feed'),
|
||||||
)
|
)
|
||||||
|
@ -1,23 +1,47 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||||
from blog.models import Post
|
from blog.models import Post
|
||||||
|
|
||||||
def mainpage(request):
|
def mainpage(request):
|
||||||
last_posts = Post.objects.filter(draft=False).order_by('-created_at')[:5]
|
last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5]
|
||||||
return render(request, 'blog/listing.html', {'posts': last_posts})
|
return render(request, 'blog/listing.html', {'posts': last_posts})
|
||||||
|
|
||||||
|
def listing(request, tag, page):
|
||||||
|
if (tag == None):
|
||||||
|
post_list = Post.objects.filter(draft = False)
|
||||||
|
view = "index"
|
||||||
|
else:
|
||||||
|
post_list = Post.objects.filter(tags__slug = tag, draft = False)
|
||||||
|
view = "tagage"
|
||||||
|
|
||||||
|
paginator = Paginator(post_list.order_by('-created_at'), 10)
|
||||||
|
|
||||||
|
try:
|
||||||
|
posts = paginator.page(page)
|
||||||
|
except PageNotAnInteger:
|
||||||
|
posts = paginator.page(1)
|
||||||
|
except EmptyPage:
|
||||||
|
posts = paginator.page(paginator.num_pages)
|
||||||
|
|
||||||
|
return render(request, 'blog/listing.html', { 'posts': posts, 'view': "blog:" + view })
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
last_posts = Post.objects.filter(draft=False).order_by('-created_at')
|
return listing(request, None, 1)
|
||||||
return render(request, 'blog/listing.html', {'posts': last_posts})
|
|
||||||
|
def indexpage(request, page):
|
||||||
|
return listing(request, None, page)
|
||||||
|
|
||||||
|
def taglist(request, tag):
|
||||||
|
return listing(request, tag, 1)
|
||||||
|
|
||||||
|
def tagpage(request, tag, page):
|
||||||
|
return listing(request, tag, page)
|
||||||
|
|
||||||
def read(request, year, month, day, slug):
|
def read(request, year, month, day, slug):
|
||||||
post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug, draft=False);
|
post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug, draft=False);
|
||||||
return render(request, 'blog/view.html', {'post': post})
|
return render(request, 'blog/view.html', {'post': post})
|
||||||
|
|
||||||
def taglist(request, tag):
|
|
||||||
posts = Post.objects.filter(tags__slug=tag, draft=False)
|
|
||||||
return render(request, 'blog/listing.html', {'posts': posts})
|
|
||||||
|
|
||||||
def feed(request):
|
def feed(request):
|
||||||
return render(request, 'blog/feed.xml', {})
|
return render(request, 'blog/feed.xml', {})
|
||||||
|
|
||||||
|
19
gergelypolonkaiweb/templates/paginator.html
Normal file
19
gergelypolonkaiweb/templates/paginator.html
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{% if list.paginator.num_pages > 1 %}
|
||||||
|
<div class="paginator">
|
||||||
|
{% if list.number > 1 %}
|
||||||
|
<a href="{% url view %}">First</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if list.has_previous and list.number != 2 %}
|
||||||
|
<a href="{% url view|add:"page" list.previous_page_number %}">Previous</a>
|
||||||
|
{% endif %}
|
||||||
|
{% for i in posts.paginator.page_range %}
|
||||||
|
{% if list.number != i %}<a href="{% url view|add:"page" i %}">{% endif %}{{ i }}{% if list.number != i %}</a>{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
{% if list.has_next and list.number != list.paginator.num_pages|add:-1 %}
|
||||||
|
<a href="{% url view|add:"page" list.next_page_number %}">Next</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if list.number < list.paginator.num_pages %}
|
||||||
|
<a href="{% url view|add:"page" list.paginator.num_pages %}">Last</a>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
Loading…
Reference in New Issue
Block a user