title: Django: How to show only the data corresponding to the current user?
body: <p>I am trying to develop a simple webpage in Django where the users shall be able to create databases via ModelForms and explore only their own created content. They shall not be able to see data created by other users. So far I was not able to realize that, each user is still able to see the data of all other users.</p>
  
  <p>In my models.py I linked my database to the User by Foreign Key. 
  I think the problem lies in my view.py  but I am somehow stuck.. </p>
  
  <p>I would be grateful for any help. 
  Thank you in advance! </p>
  
  <p>This is my <strong>models.py</strong>:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  
  CHOICES = (
      ('rent', 'Rent'), 
      ('grocery', 'Grocery'), 
      ('shopping', 'Shopping'), 
      ('gym', 'Gym'), 
      ('phone', 'Phone'), 
      ('freetime', 'Freetime'), 
      ('other', 'Other')
  )
  
  class UserDetails(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="new_spending", null=True)   
      title = models.CharField(max_length=100)
      notes = models.CharField(max_length=255)
      expense_name = models.CharField(max_length=255)
      cost = models.FloatField()
      date_added = models.DateTimeField()
      category = models.CharField(max_length=25, choices=CHOICES)
  
  
      def __str__(self):
          return self.title
  
  </code></pre>
  
  <p>My <strong>forms.py</strong>:</p>
  
  <pre><code>from django import forms
  from datetime import date
  from django.forms import ModelForm
  from .models import UserDetails
  
  
  class UserModelForm(ModelForm):
      current_date = date.today()
      YEARS = [x for x in range(current_date.year - 1, current_date.year + 6)]
      today = current_date.strftime("%Y-%m-%d")
  
      date_added = forms.DateField(initial=today, widget=forms.SelectDateWidget(years=YEARS))
  
      class Meta:
          model = UserDetails
          fields = ['title', 'notes', 'expense_name', 'cost', 'date_added', 'category']
  </code></pre>
  
  <p>My <strong>views.py</strong>: </p>
  
  <pre><code>from django.shortcuts import render
  from django.http import HttpResponse
  from django.db import models
  from .models import UserDetails
  from .forms import UserModelForm
  
  def userDetails(request):         
  
      if request.method == 'POST':   
          form = UserModelForm(request.POST)    
          if form.is_valid():
              u = form.save()
  
              users = UserDetails.objects.all()
              request.user.new_spending.add(u) 
  
              return render(request, 'budget_app/display.html', {        
                  'users': users
                  })
  
      else:
          form_class = UserModelForm
  
          return render(request, 'budget_app/userdetails.html', {
              'form': form_class,
              })
  </code></pre>
  
  <p>My <strong>html file</strong>: </p>
  
  <pre><code>
  {% extends 'budget_app/budget_base.html' %}
  
  {% block content %}
  &lt;head&gt;
  &lt;style&gt;
  table {
    width:100%;
  }
  table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
  }
  th, td {
    padding: 15px;
    text-align: left;
  }
  table#t01 tr:nth-child(even) {
    background-color: #eee;
  }
  table#t01 tr:nth-child(odd) {
   background-color: #fff;
  }
  table#t01 th {
    background-color: black;
    color: white;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;/br&gt;
  &lt;h2&gt;Your Spending History&lt;/h2&gt;
  &lt;/br&gt;
  
  &lt;table id="t01"&gt;
  
    &lt;tr&gt;
      &lt;th&gt;Title&lt;/th&gt;
      &lt;th&gt;gender&lt;/th&gt;
      &lt;th&gt;Notes&lt;/th&gt;
      &lt;th&gt;Expense name&lt;/th&gt;
      &lt;th&gt;Cost&lt;/th&gt;
      &lt;th&gt;Date&lt;/th&gt;
      &lt;th&gt;Category&lt;/th&gt;
    &lt;/tr&gt;
  
    {% for item in users %}
  
    &lt;tr&gt;
  
      &lt;td&gt;{{ item.title }}&lt;/td&gt;
      &lt;td&gt;{{ item.gender|default:"NA" }}&lt;/td&gt;
      &lt;td&gt;{{ item.notes }}&lt;/td&gt;
      &lt;td&gt;{{ item.expense_name }}&lt;/td&gt;
      &lt;td&gt;{{ item.cost }}&lt;/td&gt;
      &lt;td&gt;{{ item.date_added }}&lt;/td&gt;
      &lt;td&gt;{{ item.category }}&lt;/td&gt;
    &lt;/tr&gt;
  {% endfor %}
  &lt;/table&gt;
  &lt;br&gt;
  
  &lt;/body&gt;
  
  {% endblock %}  
  
  </code></pre>