From bb97581d650e2d994e7b2e63297dbb2d8784f5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Sun, 7 Oct 2018 23:20:39 +0300 Subject: urls for ajax next page loading --- weblog/apps.py | 2 ++ weblog/static/weblog/js/weblog.js | 3 +++ weblog/templates/weblog/index.html | 9 +++++++++ weblog/templates/weblog/nxtpage.html | 15 +++++++++++++++ weblog/urls.py | 4 ++++ weblog/views.py | 11 ++++++++++- 6 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 weblog/templates/weblog/nxtpage.html (limited to 'weblog') diff --git a/weblog/apps.py b/weblog/apps.py index cc800c1..3197cef 100755 --- a/weblog/apps.py +++ b/weblog/apps.py @@ -12,6 +12,8 @@ SETTINGS = { 'show_categories': False, 'show_archive': True, 'posts_per_page': 10, + 'dynamic_load': False, + 'infinite_load': False, 'enable_rss': True, } diff --git a/weblog/static/weblog/js/weblog.js b/weblog/static/weblog/js/weblog.js index af9b9ea..3d93770 100755 --- a/weblog/static/weblog/js/weblog.js +++ b/weblog/static/weblog/js/weblog.js @@ -11,3 +11,6 @@ function toggleNode(caller){ } target.classList.toggle('show'); } + +function loadBlogPosts(page = 2, category = null){ +} diff --git a/weblog/templates/weblog/index.html b/weblog/templates/weblog/index.html index 8dc1d93..b30c0ff 100755 --- a/weblog/templates/weblog/index.html +++ b/weblog/templates/weblog/index.html @@ -103,4 +103,13 @@ {% else %}

{% trans 'Nothing has been posted yet.' %}

{% endif %} + {% if dynamic_load %} + + {% endif %} {% endblock %} diff --git a/weblog/templates/weblog/nxtpage.html b/weblog/templates/weblog/nxtpage.html new file mode 100644 index 0000000..67d244a --- /dev/null +++ b/weblog/templates/weblog/nxtpage.html @@ -0,0 +1,15 @@ +{% load i18n %} +{% for post in posts %} +
+

{{ post.title }}

+

{% blocktrans with publish_date=post.publish_date %}Published on {{ publish_date }}{% endblocktrans %}{% if post.author %}{% blocktrans with author=post.author context 'Written by (Author)' %}, by {% endblocktrans %}{% endif %}

+
+ {% if post.preview_image %}{% endif %} + {{ post.preview_text|safe }} +
+ +
+{% endfor %} + diff --git a/weblog/urls.py b/weblog/urls.py index ddc2c9f..7ddd7f1 100755 --- a/weblog/urls.py +++ b/weblog/urls.py @@ -15,6 +15,10 @@ if blog_settings['enable_rss']: urlpatterns += [ url(r'^$', views.Index, name='Index'), + url(r'^ajaxnxtpage/page-(?P[0-9]+)/$', views.Index, name='GetPosts'), + url(r'^ajaxnxtpage/(?P[0-9]{4})/page-(?P[0-9]+)/$', views.Index, name='GetArchivePosts'), + url(r'^ajaxnxtpage/(?P[0-9]{4})/(?P[0-9]{1,2})/page-(?P[0-9]+)/$', views.Index, name='GetArchivePosts'), + url(r'^ajaxnxtpage/(?P[-\w]+)/page-(?P[0-9]+)/$', views.Index, name='GetCategoryPosts'), url(r'^change-language/(?P[-\w]+)/$', views.ChangeLanguage, name='ChangeLanguage'), url(r'^(?P[0-9]{4})/$', views.Index, name='ArchiveIndex'), url(r'^(?P[0-9]{4})/(?P[0-9]{1,2})/$', views.Index, name='ArchiveIndex'), diff --git a/weblog/views.py b/weblog/views.py index cd5f6e8..3152edc 100755 --- a/weblog/views.py +++ b/weblog/views.py @@ -1,12 +1,14 @@ from django.shortcuts import render, get_object_or_404, redirect, reverse -from django.http import Http404, HttpResponseRedirect +from django.http import Http404, HttpResponseRedirect, HttpResponse from django.conf import settings from django.utils import translation from django.utils.translation import ugettext_lazy as _, pgettext_lazy +from django.core import serializers from .apps import SETTINGS as blog_settings from .models import BlogPost, Translation, PostComment, Category, CategoryTranslation, PostCommentForm from .templatetags.weblog_extras import MONTHS import datetime +import json #Need to change the way this works later IS_MULTILINGUAL = blog_settings['multilingual'] @@ -58,6 +60,8 @@ def Index(request, **kwargs): page = int(request.GET['page'])-1 if page * POSTS_PER_PAGE + 1 > post_count: page = 0 + if 'nxtpage' in kwargs: + page = int(kwargs['nxtpage']) - 1 context_dict['current_page'] = page+1 slice_start = page*POSTS_PER_PAGE slice_end = page*POSTS_PER_PAGE + POSTS_PER_PAGE @@ -149,6 +153,11 @@ def Index(request, **kwargs): pinned_posts.append(post) else: posts.append(post) + + # If ajax is asking for the next page through the kwargs, + # just send the posts for the requested page without the other page elements + if 'nxtpage' in kwargs: + return render(request, 'weblog/nxtpage.html', { 'posts': posts }) context_dict['posts'] = posts context_dict['pinned_posts'] = pinned_posts return render(request, 'weblog/index.html', context_dict) -- cgit v1.2.3