I have created a blog with Django, I want my last published post appears at the first page. Here is my views.py code:
def index(request):
messages = get_list_or_404(Message.objects.order_by('publication_date'))
last = messages[-1]
return render_to_response('posts/index.html', {
'last_message' : last,
'posts_list' : messages,
})
But I don’t understand why, while pressing F5 on this page, it prints the last post (as expected) or the first one (without any obvious reason) randomly (sometime the last post, sometime the first one).
This behavior occurs with apache2 running with mod_wsgi and not with django development server (which displays always the last post).
Does anyone have any clue?
Many thanks
Edit: Here is the code I use in my index.html template:
{% if last_message %}
<article>
<h1><a href="/posts/{{ last_message.id }}">{{ last_message.title }}</a></h1>
<p class="meta_infos">Published on {{ last_message.publication_date }}</p>
<p>{{ last_message.text }}</p>
</article>
{% else %}
<p>No post available</p>
{% endif %}
Oki, guys, the problem came from apache server which needed to be reloaded (there is no need to do so with PHP, that’s why I haven’t thought of it).
I also have replaced
messages = get_list_or_404(Message.objects.order_by(‘publication_date’))
by
last = get_list_or_404(Message).latest(‘publication_date’)
Which is more concise.
Thanks for your help and answers!