Я работаю над системой управления счетами, в которой пользователь может добавлять данные счетов-фактур, и они будут сохраняться в базе данных, и всякий раз, когда пользователь входит в систему, данные будут отображаться на домашней странице. данные других пользователей также, но я хочу только данные активных пользователей. не могли бы вы, ребята, помочь мне.
вот моя домашняя страница, вы можете видеть, что я вошел через пользователя 1, но на домашней странице также отображаются данные пользователя 2
вот мой код
views.py
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' # <app>/<model>_<viewtype>.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'})
models.py
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})
home. html
{% extends "invoicedata/base.html" %}
{% block content %}
{% for invoice in invoices %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<h2><a class="article-title" href="{% url 'invoice-detail' invoice.id %}">{{ invoice.issuer }}</a></h2>
</div>
<p class="article-content">{{ invoice.invoice_number }}</p>
<p class="article-content">{{ invoice.date }}</p>
<p class="article-content">{{ invoice.amount }}</p>
<p class="article-content">{{ invoice.currency }}</p>
<p class="article-content">{{ invoice.other }}</p>
<div class="article-metadata">
<small class="text-muted">{{ invoice.author }}</small>
</div>
</div>
</article>
{% endfor %}
{% endblock content %}
invoicelist_deatil. html
{% extends "invoicedata/base.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.date }}</small>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'invoice-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'invoice-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-issuer">{{ object.issuer }}</h2>
<p class="article-invoice_number">{{ object.invoice_number }}</p>
<p class="article-date">{{ object.date }}</p>
<p class="article-amount">{{ object.amount }}</p>
<p class="article-currency">{{ object.currency }}</p>
<p class="article-other">{{ object.other }}</p>
</div>
</article>
{% endblock content %}