Pagination on DjangO - No Values - python

I am following a specific guide and basically hit a roadblock I cannot debug.
I implemented Paginator into my blog site and was hoping to show the current page / end of page.
But for some reason the page values are blank.
What would you think is the reason behind this issue? Do you think that this has something to do with my VS compiler not importing paginator properly?
list HTML
pagination block code
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Previous
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
Next
{% endif %}
</span>
</div>
views.py
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Post
# Create your views here.
def post_list(request):
object_list = Post.published.all()
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,
'blog/post/detail.html',
{'post': post})
List HTML Code
{% extends "blog/base.html" %}
{% block title %} My Blog {% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=post %}
{% endblock %}
Github Link

Try using page=posts rather than page=post.
{% include "pagination.html" with page=posts %}

Related

Pagination stopped working on Django application

I am new to Django and I just picked up a book "Django 2 by example". I followed all the code in the tutorial bu something broke the pagination along the line and I just can't figure what it is. I get an output of "Page of ." instead of "Page 1 of 1. at the base of my blog. Find some of my code below:. P.S: Kindly ignore the indentation in the code. Thank you.
Views.py
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from django.core.mail import send_mail
from django.db.models import Count
from taggit.models import Tag
from .models import Post, Comment
from .forms import EmailPostForm, CommentForm
def post_list(request, tag_slug=None):
object_list = Post.published.all()
tag = None`
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])
paginator = Paginator(object_list, 3) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'page': page,
'posts': posts,
'tag' : tag})
list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% if tag %}
<h2>Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url "blog:post_list_by_tag" tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
Pagination.html (template)
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
Previous
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages}}.
</span>
{% if page.has_next %}
Next
{% endif %}
</span>
</div>

Django - Exclude current post from side bar

I am playing with Django but I'm having trouble with my templates. In my post detail in including a recent posts side bar from a template tag but I want to exclude the current post if it is, in fact, one of the most recent. My original hope was to use .exclude(id__post_detail=post) in the blog_tag but I think I might be missing an important concept, do I need to request post_detail return its response and then I can do that? Or perhaps define the query in the view and then call it to the blog_tag?
Many thanks in advance.
1.blog_tags.py
from django import template
register = template.Library()
from django.db import models
from django.utils import timezone
from ..models import Post
#register.inclusion_tag('blog/sidebar.html')
def sidebar_latest(request, count=5):
latest_posts= Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[:count]
return {'latest_posts': latest_posts}
views
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
post_detail
{% extends 'blog/base.html' %}
{% load blog_tags %}
{% block content %}
<div class="col-sm-12 col-md-9">
<div class="post">
<h1>{{ post.title }}</h1>
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<p>{{ post.text|linebreaksbr }}</p>
</div>
</div>
<div class="col-sm-12 col-md-3">
{% sidebar_latest 3 %}
</div>
{% endblock %}

Wrong posts sequence at home page [Django app]

At my home-page I've got exhibited all posts on my blog, but they're sorted incorrectly, from the oldest post to newest(it has to be reversed).
I use querysets to sort posts order by published date in my views.py
def home(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, "home.html", {'posts': posts})
And that's my home.html source code:
{% extends "C:\myapp\blog\templates\base.html" %}
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock content %}
Could you help me in reverse these posts?
Thanks in advance.
You want to add a - to the string argument in order_by this will cause your queryset to be in descending order.
def home(request):
posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
return render(request, "home.html", {'posts': posts})
Notice the .order_by('-published_date')

I do not understand this Django error

Error during template rendering
In template
C:\Users\Paddy\Desktop\Django-tut\mysite\blog\templates\blog\post_list.html,
error at line 10 Reverse for 'post_detail' with arguments '()' and
keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried:
['blog/post/(?P[0-9]+)/$']
{% extends 'blog/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h1>{{ post.title }}</h1>
<!--<h1>{{ post.title }}</h1>-->
<p>{{ post.text|linebreaks }}</p>
</div>
{% endblock %}
My post_detail.html file looks like this
{% extends 'blog/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endblock %}
My urls.py is
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
]
and views.py is
from django.shortcuts import render
from django.utils import timezone
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
#Post.objects.get(pk=pk)
# Create your views here.
def post_list(request):
return render(request, 'blog/post_list.html', {})
Thanks.
Assuming the first template you've posted is post_list.html you don't send any context variables to it.
In post_list view - if you want to list all posts - you have to add:
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
Then in your post_list.html template you have to loop over posts:
{% extends 'blog/base.html' %}
{% block content %}
{% for post in posts %} # iterate over posts
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endfor %}
{% endblock %}
There error message complains that it can't find a reverse URL when pk is '', the empty string.
Indeed there is no URL that matches that, as the regular expression required [0-9]+, and the empty string doesn't match that. So a reverse match can't be found.
The reason why pk was empty is explained in the other answer.

Flask mongoengine pagination

I have a small Flask app which renders blog posts:
views.py:
class ListView(MethodView):
def get(self, page=1):
posts = Post.objects.all()
return render_template('posts/list.html', posts=posts)
This is all good, but I would like to add pagination to the posts object. Looking at the project docs, I see there is a pagination class.
So I tried this:
class ListView(MethodView):
def get(self, page=1):
posts = Post.objects.paginate(page=page, per_page=10)
return render_template('posts/list.html', posts=posts)
But now I get an error:
TypeError: 'Pagination' object is not iterable
So how do I iterate through my posts in the template?
The Pagination object has an items list which will contain the mongoengine document objects (in your case the Post objects). This list can be iterated through to display the documents.
For example, in your template:
{% for post in posts.items %}
{{ post.title }}
{{ post.content }}
{% endfor %}
To get the actual page numbers for pagination links, use iter_pages():
<div id="pagination-links">
{% for page in posts.iter_pages() %}
{{ page }}
{% endfor %}
</div>
Both the documentation and the github link above, have a better example for pagination links:
{% macro render_pagination(pagination, endpoint) %}
<div class=pagination>
{%- for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
{{ page }}
{% else %}
<strong>{{ page }}</strong>
{% endif %}
{% else %}
<span class=ellipsis>…</span>
{% endif %}
{%- endfor %}
</div>
{% endmacro %}

Categories