Jungletronics
Published in

Jungletronics

A Django Blog In VS Code — Posts Pagination & Quick DB Population

How To Create A Blog in VS Code — Part IX — DjangoSeries Episode # 12

Themes : PAGINATION & POST FILTERED
Hurrah! That means our website isn't going to waste time loading a desktop-sized image and lots of posts for your mobile website.
(djangoEnv) C:\Users\giljr\new_django\django_project>python manage.py shell
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
...
... posts_json = json.load(f)
...
>>> for post in posts_json:
... post = Post(title=post['title'], content=post['content'], author_id=post['user_id'])
... post.save()
...
>>> exit()
python manage.py runserver
We upload 24 posts to the database.
Now we can play with paginator…Let’s get on!
(djangoEnv) C:\Users\giljr\new_django\django_project>python manage.py shell
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.paginator import Paginator
>>> posts = ['1','2','3','4','5']
>>> p = Paginator(posts, 2)
>>> p.num_pages
3
>>>
>>> for page in p.page_range:
... print(page)
...
1
2
3
>>> p1 = p.page(1)
>>> p1
<Page 1 of 3>
>>> p1.number
1
>>> p1.object_list
['1', '2']
>>> p1.has_previous()
False
>>> p1.has_next()
True
>>> p1.next_page_number()
2
>>> exit()
...class PostListView(ListView):
model = Post
# context_object_name = 'object_list'
ordering = ['-date_posted']
paginate_by = 2
...
http://127.0.0.1:8000/?page=3
And magically you have paginator up and running \o/ As we have 28 post/2 = 14 pages
This is the last page…let’s confirm…
As you can see here…oops! 404! anytime you run out of pages and requests that page that doesn’t exist then you will get a 404 which is good!
Pagination script is inside {% if is_paginated %}…{% endif %} tags.
python manage.py runserver
CBV PostUserListView(ListView)
The difference between the old code is the creation of class PostUserListView(ListView) and some importations
from blog.views import ... PostUserListViewurlpatterns = [
...
path('user/<str:username>/', PostUserListView.as_view(), name='post_list'),
...
]
The difference between the old code is the insertion of a new path.
...
<div class="media-body">
<div class="article-metadata">
<a class="font-weight-bold mr-2" href="{% url 'post_list' object.author.username %}">{{ object.author }}</a>
...
...
<div class="media-body">
<div class="article-metadata">
<a class="font-weight-bold mr-2" href="{% url 'post_list' object.author.username %}">{{ object.author }}</a>
<small ... </small>
</div class="mb-4">
<h1 class="display-6"><strong><a href="{% url 'post_detail' object.id %}">{{ object.title }}</a></strong></h1>
<br></br>
...
{% endfor %}
...
<<<< paginate script goes here see in step#07 >>>
python manage.py runserver
The informational message appears every time you click on the author of the post. The filter worked!

References & Credits

Related Posts

Here is the step-by-step —Django Unleashed by me o(* ̄▽ ̄*)ブ

--

--

J of Jungle + 3 Plats Arduino/RPi/Pic = J3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
J3

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!