title: Django : How to show logged in users data from database
body: <p>I am working on invoice management system having in which user can add invoice data and it will save in database and whenever user logged in the data will appear on home page.but the problem if whenever any user logged in the home page showing other users data also  but i want active users data only. could you guys help me.</p>
  
  <p>here is my home page,you can see i am logged in through user 1 but at home page user 2 data also appear
  <a href="https://i.stack.imgur.com/lwEpc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lwEpc.png" alt="enter image description here"></a> </p>
  
  <p>here is my code </p>
  
  <p>views.py</p>
  
  <pre><code>
  from django.shortcuts import render
  from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
  from django.views.generic import (
      ListView,
      DetailView,
      CreateView,
      UpdateView,
      DeleteView
  )
  from .models import Invoicelist
  
  def home(request):
      context = {
          'invoices': Invoicelist.objects.all()
      }
      return render(request, 'invoicedata/home.html', context)
  
  
  class InvoiceListView(ListView):
      model = Invoicelist
      template_name = 'invoicedata/home.html'  # &lt;app&gt;/&lt;model&gt;_&lt;viewtype&gt;.html
      context_object_name = 'invoices'
  
  
  class InvoiceDetailView(DetailView):
      model = Invoicelist
  
  
  class InvoiceCreateView(LoginRequiredMixin, CreateView):
      model = Invoicelist
      fields = ['issuer','invoice_number','date','amount','currency','other']
  
      def form_valid(self, form):
          form.instance.author = self.request.user
          return super().form_valid(form)
  
  
  class InvoiceUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
      model = Invoicelist
      fields = ['issuer','invoice_number','date','amount','currency','other']
  
      def form_valid(self, form):
          form.instance.author = self.request.user
          return super().form_valid(form)
  
      def test_func(self):
          invoice = self.get_object()
          if self.request.user == invoice.author:
              return True
          return False
  
  
  class InvoiceDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
      model = Invoicelist
      success_url = '/'
  
      def test_func(self):
          invoice = self.get_object()
          if self.request.user == invoice.author:
              return True
          return False
  
  
  
  def about(request):
      return render(request, 'invoicedata/about.html', {'title': 'About'})
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from django.utils import timezone
  from django.urls import reverse
  
  class Invoicelist(models.Model):
      issuer = models.CharField(max_length=50)
      invoice_number = models.CharField(max_length=50)
      date = models.CharField(max_length=50)
      amount = models.IntegerField()
      currency = models.CharField(max_length=10)
      other = models.TextField()
      author = models.ForeignKey(User, on_delete=models.CASCADE)
  
      def __str__(self):
          return (self.issuer)
  
      def get_absolute_url(self):
          return reverse('invoice-detail', kwargs={'pk': self.pk})
  </code></pre>
  
  <p>home.html</p>
  
  <pre><code>{% extends "invoicedata/base.html" %}
  {% block content %}
      {% for invoice in invoices %}
          &lt;article class="media content-section"&gt;
            &lt;div class="media-body"&gt;
              &lt;div class="article-metadata"&gt;
                &lt;h2&gt;&lt;a class="article-title" href="{% url 'invoice-detail' invoice.id %}"&gt;{{ invoice.issuer }}&lt;/a&gt;&lt;/h2&gt;
              &lt;/div&gt;
  
              &lt;p class="article-content"&gt;{{ invoice.invoice_number }}&lt;/p&gt;
              &lt;p class="article-content"&gt;{{ invoice.date }}&lt;/p&gt;
              &lt;p class="article-content"&gt;{{ invoice.amount }}&lt;/p&gt;
              &lt;p class="article-content"&gt;{{ invoice.currency }}&lt;/p&gt;
              &lt;p class="article-content"&gt;{{ invoice.other }}&lt;/p&gt;
              &lt;div class="article-metadata"&gt;
                &lt;small class="text-muted"&gt;{{ invoice.author }}&lt;/small&gt;
              &lt;/div&gt;
  
            &lt;/div&gt;
          &lt;/article&gt;
      {% endfor %}
  {% endblock content %}
  </code></pre>
  
  <p>invoicelist_deatil.html</p>
  
  <pre><code>{% extends "invoicedata/base.html" %}
  {% block content %}
    &lt;article class="media content-section"&gt;
      &lt;img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}"&gt;
      &lt;div class="media-body"&gt;
        &lt;div class="article-metadata"&gt;
          &lt;a class="mr-2" href="#"&gt;{{ object.author }}&lt;/a&gt;
          &lt;small class="text-muted"&gt;{{ object.date }}&lt;/small&gt;
          {% if object.author == user %}
            &lt;div&gt;
              &lt;a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'invoice-update' object.id %}"&gt;Update&lt;/a&gt;
              &lt;a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'invoice-delete' object.id %}"&gt;Delete&lt;/a&gt;
            &lt;/div&gt;
          {% endif %}
        &lt;/div&gt;
        &lt;h2 class="article-issuer"&gt;{{ object.issuer }}&lt;/h2&gt;
        &lt;p class="article-invoice_number"&gt;{{ object.invoice_number }}&lt;/p&gt;
        &lt;p class="article-date"&gt;{{ object.date }}&lt;/p&gt;
        &lt;p class="article-amount"&gt;{{ object.amount }}&lt;/p&gt;
        &lt;p class="article-currency"&gt;{{ object.currency }}&lt;/p&gt;
        &lt;p class="article-other"&gt;{{ object.other }}&lt;/p&gt;
      &lt;/div&gt;
    &lt;/article&gt;
  {% endblock content %}
  </code></pre>