make pagination work
This commit is contained in:
parent
d81e709827
commit
dafa5ca517
@ -6,7 +6,7 @@ urlpatterns = [
|
||||
url(r'^ctl-(?P<page>[0-9]+)\.html$', views.modlog, name='old-modlog'),
|
||||
url(r'^ctl/((?P<page>[0-9]+)/)?$', views.modlog, name='modlog'),
|
||||
url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)-(?P<page>[0-9]+)\.html$', views.BoardView.as_view(), name='old-board'),
|
||||
url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)/((?P<page>[0-9]+)/)?$', views.BoardView.as_view(), name='board'),
|
||||
url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)/', views.BoardView.as_view(), name='board'),
|
||||
url(r'^thread-(?P<op>[a-fA-F0-9\.]{40})\.html$', views.ThreadView.as_view(), name='old-thread'),
|
||||
url(r'^t/(?P<op>[a-fA-F0-9\.]{40})/$', views.ThreadView.as_view(), name='thread'),
|
||||
url(r'^$', views.FrontPageView.as_view(), name='index'),
|
||||
|
@ -1,7 +1,9 @@
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.views import generic
|
||||
|
||||
|
||||
from .models import Post, Newsgroup
|
||||
|
||||
class BoardView(generic.View):
|
||||
@ -9,9 +11,17 @@ class BoardView(generic.View):
|
||||
context_object_name = 'threads'
|
||||
model = Post
|
||||
|
||||
def get(self, request, name, page):
|
||||
def get(self, request, name):
|
||||
page = 0
|
||||
if 'p' in request.GET:
|
||||
page = request.GET['p']
|
||||
newsgroup = 'overchan.{}'.format(name)
|
||||
try:
|
||||
page = int(page or "0")
|
||||
except:
|
||||
page = 0
|
||||
if page < 0:
|
||||
page = 0
|
||||
try:
|
||||
group = Newsgroup.objects.get(name=newsgroup)
|
||||
except Newsgroup.DoesNotExist:
|
||||
@ -20,11 +30,11 @@ class BoardView(generic.View):
|
||||
begin = page * group.posts_per_page
|
||||
end = begin + group.posts_per_page - 1
|
||||
roots = self.model.objects.filter(newsgroup=group, reference='').order_by('-last_bumped')[begin:end]
|
||||
ctx = {'threads': roots,'page': page, 'name': newsgroup}
|
||||
ctx = {'threads': roots, 'page': page, 'name': newsgroup}
|
||||
if page < group.max_pages:
|
||||
ctx['nextpage'] = '/{}/{}/'.format(group.name, page + 1)
|
||||
ctx['nextpage'] = reverse('board', args=[name]) + '?p={}'.format(page + 1)
|
||||
if page > 0:
|
||||
ctx['prevpage'] = '/{}/{}/'.format(group.name, page - 1)
|
||||
ctx['prevpage'] = reverse('board', args=[name]) + '?p={}'.format(page - 1)
|
||||
return render(request, self.template_name, ctx)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user