title: How can I show my last 3 posts with Django Python
body: <p>I try to show my last 3 posts with Django but my three posts doesn't appear on my page. There is my code.
  I have no error or anything when I load the page and my other posts apper on my page.
  And please be kind it's the first time I use Python and Django =)</p>
  
  <h2>models.py</h2>
  
  <pre><code>from django.db import models
  from django.utils import timezone
  
  class Category(models.Model):
      name = models.CharField(max_length=200)
  
      def __str__(self):
          return self.name
  
  class Post(models.Model):
      author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
      title = models.CharField(max_length=200)
      text = models.TextField()
      created_date = models.DateTimeField(default=timezone.now)
      published_date = models.DateTimeField(
      blank=True, null=True)
      category = models.ForeignKey('Category')
  
      def publish(self):
          self.published_date = timezone.now()
          self.save()
  
      def __str__(self):
          return self.title
  </code></pre>
  
  <h2>views</h2>
  
  <pre><code>from django.shortcuts import render
  from django.utils import timezone
  from .models import Category, Post
  
  def category_list(request):
      categories = Category.objects.all()
      return render (request, 'blog/post_list.html', {'categories': categories})
  
  def post_list(request):
  posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
  latests = Post.objects.filter(published_date__lte=timezone.now()).reverse()[:3]
  return render(request, 'blog/post_list.html', {'posts': posts})
  return render(request, 'blog/post_list.html', {'latests': latests})
  </code></pre>
  
  <h2>My urls</h2>
  
  <pre><code>from django.conf.urls import url
  from . import views
  
  urlpatterns = [
      url(r'^$', views.post_list, name='post_list'),
  ]
  </code></pre>
  
  <h2>template html (post_list.html)</h2>
  
  <pre><code>        &lt;div id="container"&gt;
          {% for latest in latests %}
          &lt;article class="lastnews"&gt;
              &lt;h4&gt;{{ latest.title }}&lt;/h4&gt;
              &lt;h5 class="lastestcategory"&gt;{{ latest.category }}&lt;/h5&gt;
              &lt;p class="bodysmall"&gt;{{ latest.text|truncatewords:10 }}&lt;/p&gt;
              &lt;div&gt;
                  &lt;p class="date"&gt;{{ latest.published_date }}&lt;/p&gt;
                  &lt;p class="showmore"&gt;&lt;a href=""&gt;Show more&lt;/a&gt;&lt;/p&gt;
              &lt;/div&gt;
          &lt;/article&gt;
          {% endfor %}
      &lt;/div&gt;
  &lt;/section&gt;
  &lt;section id="posts"&gt;
      {% for post in posts %}
      &lt;article class="post"&gt;
          &lt;header class="postheader"&gt;
              &lt;h4&gt;{{ post.title }}&lt;/h4&gt;
              &lt;p class="info"&gt;{{ post.category }}, {{ post.published_date }}&lt;/p&gt;
          &lt;/header&gt;
          &lt;p class="bodyregular"&gt;{{ post.text|linebreaksbr }}&lt;/p&gt;
          &lt;footer class="postfooter"&gt;
              &lt;p class="author"&gt;Author: {{ post.author }}&lt;/p&gt;
              &lt;p class="showarticle"&gt;&lt;a href=""&gt;Show article&lt;/a&gt;&lt;/p&gt;
          &lt;/footer&gt;
      &lt;/article&gt;
      {% endfor %}
  &lt;/section&gt;
  </code></pre>
  
  <p>Thanks for your help =)</p>
  
  <p>EDIT</p>
  
  <p>I fixed my view in this way</p>
  
  <pre><code>def latest_posts(request):
  latests = Post.objects.filter(published_date__lte=timezone.now()).reverse()[:3]
  return render(request, 'blog/post_list.html', {'latests': latests})
  </code></pre>
  
  <p>but the three posts doesn't appear on my page</p>