Commit 7b144b15a6a96853ea8ec90c7bfd02a7e529f814
1 parent
bc259b46
Exists in
master
and in
5 other branches
Adding post pagination (Load more posts button) [Issue: #74]
Showing
6 changed files
with
100 additions
and
2 deletions
Show diff stats
forum/static/js/forum.js
... | ... | @@ -220,6 +220,39 @@ function delete_post(url, post) { |
220 | 220 | |
221 | 221 | /* |
222 | 222 | * |
223 | +* Function to load more posts | |
224 | +* | |
225 | +*/ | |
226 | +function load_more_posts(pageNum, numberPages, url) { | |
227 | + // Remove button from the template | |
228 | + $("#load_more_posts").remove(); | |
229 | + | |
230 | + // Check if page is equal to the number of pages | |
231 | + if (pageNum == numberPages) { | |
232 | + return false | |
233 | + } | |
234 | + | |
235 | + pageNum += 1; | |
236 | + | |
237 | + // Show loader | |
238 | + $("#loading_posts").show(); | |
239 | + | |
240 | + // Configure the url we're about to hit | |
241 | + setTimeout(function (){ | |
242 | + $.ajax({ | |
243 | + url: url, | |
244 | + data: {'page': pageNum}, | |
245 | + success: function(data) { | |
246 | + $("#loading_posts").hide(); | |
247 | + | |
248 | + $("#posts_list").append(data); | |
249 | + } | |
250 | + }); | |
251 | + }, 1000) | |
252 | +}; | |
253 | + | |
254 | +/* | |
255 | +* | |
223 | 256 | * Function to load answer post form and set the submit function |
224 | 257 | * |
225 | 258 | */ |
... | ... | @@ -321,4 +354,5 @@ function delete_answer(url, answer, message) { |
321 | 354 | } |
322 | 355 | }); |
323 | 356 | }); |
324 | -} | |
325 | 357 | \ No newline at end of file |
358 | +} | |
359 | + | ... | ... |
forum/templates/forum/forum_view.html
... | ... | @@ -101,6 +101,11 @@ |
101 | 101 | <div id="posts_list"> |
102 | 102 | {% list_posts request forum %} |
103 | 103 | </div> |
104 | + <div id="loading_posts" class="alert alert-primary" role="alert" style="display: none"> | |
105 | + <center> | |
106 | + <span class="fa fa-spin fa-circle-o-notch"></span> | |
107 | + </center> | |
108 | + </div> | |
104 | 109 | </div> |
105 | 110 | </div> |
106 | 111 | <!-- Modal to show Forum edit form --> | ... | ... |
forum/templates/post/post_list.html
... | ... | @@ -42,4 +42,10 @@ |
42 | 42 | </div> |
43 | 43 | </div> |
44 | 44 | {% endfor %} |
45 | +{% endif %} | |
46 | + | |
47 | +{% if not page_obj.number == paginator.num_pages %} | |
48 | + <a id="load_more_posts" href="javascript:load_more_posts({{ page_obj.number }}, {{ paginator.num_pages }}, '{% url 'course:forum:load_posts' forum_id=forum.id %}');" class="btn btn-raised btn-primary btn-block"> | |
49 | + {% trans 'Load more posts' %} | |
50 | + </a> | |
45 | 51 | {% endif %} |
46 | 52 | \ No newline at end of file | ... | ... |
forum/templatetags/list_post.py
1 | 1 | from django import template |
2 | 2 | |
3 | +from django.core.paginator import Paginator, EmptyPage | |
4 | + | |
3 | 5 | from forum.models import Post |
4 | 6 | |
5 | 7 | register = template.Library() |
... | ... | @@ -14,6 +16,24 @@ def list_posts(request, forum): |
14 | 16 | 'request': request, |
15 | 17 | } |
16 | 18 | |
17 | - context['posts'] = Post.objects.filter(forum = forum).order_by('post_date') | |
19 | + posts = Post.objects.filter(forum = forum).order_by('post_date') | |
20 | + | |
21 | + paginator = Paginator(posts, 1) | |
22 | + | |
23 | + try: | |
24 | + page_number = int(request.GET.get('page', 1)) | |
25 | + except ValueError: | |
26 | + raise Http404 | |
27 | + | |
28 | + try: | |
29 | + page_obj = paginator.page(page_number) | |
30 | + except EmptyPage: | |
31 | + raise Http404 | |
32 | + | |
33 | + context['paginator'] = paginator | |
34 | + context['page_obj'] = page_obj | |
35 | + | |
36 | + context['posts'] = page_obj.object_list | |
37 | + context['forum'] = forum | |
18 | 38 | |
19 | 39 | return context |
20 | 40 | \ No newline at end of file | ... | ... |
forum/urls.py
... | ... | @@ -11,6 +11,7 @@ urlpatterns = [ |
11 | 11 | url(r'^render_forum/([\w_-]+)/$', views.render_forum, name='render_forum'), |
12 | 12 | url(r'^render_edit_forum/([\w_-]+)/$', views.render_edit_forum, name='render_edit_forum'), |
13 | 13 | url(r'^forum_deleted/$', views.forum_deleted, name='deleted_forum'), |
14 | + url(r'^load_posts/(?P<forum_id>[\w_-]+)/$', views.load_posts, name='load_posts'), | |
14 | 15 | url(r'^create_post/$', views.CreatePostView.as_view(), name='create_post'), |
15 | 16 | url(r'^update_post/(?P<pk>[\w_-]+)/$', views.PostUpdateView.as_view(), name='update_post'), |
16 | 17 | url(r'^delete_post/(?P<pk>[\w_-]+)/$', views.PostDeleteView.as_view(), name='delete_post'), | ... | ... |
forum/views.py
... | ... | @@ -4,6 +4,8 @@ from django.core.urlresolvers import reverse, reverse_lazy |
4 | 4 | from django.utils.translation import ugettext_lazy as _ |
5 | 5 | from django.views import generic |
6 | 6 | from django.contrib.auth.mixins import LoginRequiredMixin |
7 | +from django.core.paginator import Paginator, EmptyPage | |
8 | +from django.http import Http404 | |
7 | 9 | |
8 | 10 | from .models import Forum, Post, PostAnswer |
9 | 11 | from courses.models import Topic |
... | ... | @@ -110,6 +112,36 @@ class ForumDetailView(LoginRequiredMixin, generic.DetailView): |
110 | 112 | """ |
111 | 113 | Post Section |
112 | 114 | """ |
115 | +def load_posts(request, forum_id): | |
116 | + context = { | |
117 | + 'request': request, | |
118 | + } | |
119 | + | |
120 | + forum = get_object_or_404(Forum, id = forum_id) | |
121 | + | |
122 | + posts = Post.objects.filter(forum = forum).order_by('post_date') | |
123 | + | |
124 | + paginator = Paginator(posts, 1) | |
125 | + | |
126 | + try: | |
127 | + page_number = int(request.GET.get('page', 1)) | |
128 | + except ValueError: | |
129 | + raise Http404 | |
130 | + | |
131 | + try: | |
132 | + page_obj = paginator.page(page_number) | |
133 | + except EmptyPage: | |
134 | + raise Http404 | |
135 | + | |
136 | + print(page_number) | |
137 | + | |
138 | + context['paginator'] = paginator | |
139 | + context['page_obj'] = page_obj | |
140 | + | |
141 | + context['posts'] = page_obj.object_list | |
142 | + context['forum'] = forum | |
143 | + | |
144 | + return render(request, 'post/post_list.html', context) | |
113 | 145 | |
114 | 146 | class CreatePostView(LoginRequiredMixin, generic.edit.CreateView): |
115 | 147 | login_url = reverse_lazy("core:home") | ... | ... |