我正在开发一个发票管理系统,用户可以添加发票数据并保存到数据库中。每当用户登录时,该数据将显示在主页上。但是,每当用户注销并尝试访问主页时,却出现以下错误。
TypeError at /
'AnonymousUser' object is not iterable
我尝试了AnonymousUser.is_authenticated
方法,但仍然无法工作。
如果用户已登录,则应打开home.html
,否则应打开intro.html
这是我的代码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):
if request.user.is_authenticated():
context = {
'invoices': Invoicelist.objects.all()
}
return render(request, 'invoicedata/home.html', context)
else:
return render(request, 'invoicedata/intro.html', context)
主页.html
{% extends "invoicedata/base.html" %}
{% block content %}
{% for invoice in invoices %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<small class="text-muted">{{ invoice.date_posted|date:"F d, Y" }}</small>
<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 %}
介绍.html
{% extends "invoicedata/base.html" %}
{% block content %}
<h2>login to your portal for great auditing services</h2>
{% endblock content %}