Wrong posts sequence at home page [Django app] - python

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')

Related

Pagination on DjangO - No Values

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 %}

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 %}

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.

Django redirect back to previous page on form post

I'm trying to have a form post redirect back to the page it was posted from. So my app has a detail page that displays information about a place, /arena/123, and on the same page there is a form that a user can enter some comments or ratings about the current place they are looking at. These are my view functions.
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Arena
template_name = 'arenas/detail.html'
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
if 'form' not in context:
print 'no form in context'
context['form'] = RatingForm
return context
def rate(request):
if request.method == 'POST':
form = RatingForm(request.POST)
if form.is_valid():
# post was valid. go back to page and display data
return DetailView.as_view()(request(), form=RatingForm)
else:
# post was unsuccessful. go back to the page and display errors
print form.errors
return DetailView.as_view()(request(), form=form)
and my template
{% extends "base.html" %}
{% block content %}
<h1>{{ arena.address }}</h1>
<h1>{{ arena.address_2 }}</h1>
<h1>{{ arena.phone }}</h1>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form action="/arenas/rate/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<div>
{{comments}}
</div>
{% endblock %}
So what I'd like to do is after a user submits the form on the page I'd like to go back to the same detail page I was on and either have the data that user posted there shown in the comments section if it was successful or redirect back to the same page but with the form errors if it was unsuccessful. My problem is right now I don't know how to retain the url and form errors to display the detail page again. I don't think I can use return DetailView.as_view()(context(), form=form) because my DetailView doesn't have a post so I get a 405. Every example I see doesn't cover redirecting back to the same page. They usually just show a HttpResponseRedirect(/thanks) or something similar.
Also note. I don't want to post to /arena/123 because eventually that will be another feature to update information about the place so I didn't want to put the comments post on that url.
So I dug into some more Django examples and stumbled across SingleObjectMixin. This combined with a FormView seems to be what I want. The Django docs had an example that was 99.9 percent what I wanted here. Just be sure to read down to the better solution section.
I changed my views to this
class DetailView(LoginRequiredMixin, generic.DetailView):
model = Arena
template_name = 'arenas/detail.html'
def get_context_data(self, **kwargs):
print kwargs
context = super(DetailView, self).get_context_data(**kwargs)
context['form'] = RatingForm
return context
class RatingView(LoginRequiredMixin, detail.SingleObjectMixin, generic.FormView):
model = Arena
template_name = 'arenas/detail.html'
form_class = RatingForm
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super(RatingView, self).post(request, *args, **kwargs)
def get_success_url(self):
print "successfully posted"
return reverse('arenas:detail', kwargs={'pk': self.object.pk})
Added a route for posting the form with /rate
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$',
views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/rate/$', views.RatingView.as_view(), name='rate')
)
and modified my template a bit to pass the id of the object to my route in the form post action
{% extends "base.html" %}
{% block content %}
<h1>{{ arena.address }}</h1>
<h1>{{ arena.address_2 }}</h1>
<h1>{{ arena.phone }}</h1>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form action="/arenas/{{ arena.id }}/rate/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<div>
{{comments}}
</div>
{% endblock %}
Now on an error the context is kept and the SingleObjectMixin/FormView combination keeps me on the same page to display the form errors and on a successful post it redirects to the detail page I want using the get_success_url to load the page again with the new information that was posted.

Django-likes repeats the same post

I'm using the app django-likes and have managed to get it working. But I'm running into a very weird problem. The model "Story" is associated with the likes. Now the Story posts are repeated the number of votes they have -- for example, if they have 4 votes, they're actually repeated 4 times. Not sure what's going on. Any help much appreciated.
views.py
def home(request):
posts = Story.objects.all().order_by('votes')
form = StoryForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Thanks for adding your mix!')
return render_to_response('home.html',
locals(),
context_instance=RequestContext(request))
models.py
from django.db import models
import secretballot, likes
class Story(models.Model):
text = models.CharField(max_length=2000)
def __unicode__(self):
return self.text
secretballot.enable_voting_on(Story)
home.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load likes_inclusion_tags %}
{% block content %}
{% if user.is_authenticated %}
<h1 align=center>Add Your Mix!</h1>
<div class='col-sm-6 col-sm-offset-3'>
<h1>{{ form_title }}</h1>
<form method="POST" action="" >{% csrf_token %}
{{ form|crispy }}
<input type='submit' class='btn' value='Add{{ button_submit }}' />
</form>
</div>
{% else %}
<h1 align=center>Login and add a story!</h1>
<p align=center><a class="btn btn-primary btn-lg" href="/accounts/login/" role="button">Login!</a></p>
{% endif %}
{% for post in posts %}
<p>{{ post.text }} {% likes post %}</p>
{% endfor %}
{% endblock %}
It is possible that the {% likes post %}returns a number and Multiplies the string {{ post.text }} five times like python would. Try separating it or converting it to string.

Categories