May*_*tav 15 python django django-templates django-views
我正在开发发票管理系统,用户可以在其中添加发票数据并将其保存在数据库中,每当用户登录时,数据都会出现在主页上,但每当用户注销并尝试访问主页时,它都会出现以下错误。
TypeError at /
'AnonymousUser' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我尝试了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)
Run Code Online (Sandbox Code Playgroud)
主页.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 %}
Run Code Online (Sandbox Code Playgroud)
介绍.html
{% extends "invoicedata/base.html" %}
{% block content %}
<h2>login to your portal for great auditing services</h2>
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)
May*_*tav 11
最后我得到了适合我的解决方案
这里是
Django 提供LoginRequiredMixin我在我的 invoicelistview 函数中使用了它
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
class InvoiceListView(LoginRequiredMixin,ListView):
model = Invoicelist
template_name = 'invoicedata/home.html'
context_object_name = 'invoices'
def get_queryset(self):
return self.model.objects.all().filter(author=self.request.user).order_by('-date_posted')[:2]
Run Code Online (Sandbox Code Playgroud)
就是这样。现在每当用户注销时,它将重定向到登录页面
我知道这个问题已经回答了,我只想总结一下隐藏/显示信息给非认证用户的每一种方法。
1.登录需要装饰器
如果您正在处理功能视图,您可以像这样装饰它:
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
pass
Run Code Online (Sandbox Code Playgroud)
这只会向经过身份验证的用户显示视图。如果匿名,他们将被重定向到登录 URL (settings.LOGIN_URL)
2. LoginRequiredMixin
from django.contrib.auth.mixins import LoginRequiredMixin
class MyView(LoginRequiredMixin, View):
login_url = '/login/'
redirect_field_name = 'redirect_to'
Run Code Online (Sandbox Code Playgroud)
这是基于类的视图。来自 Django 文档:
如果视图正在使用此 mixin,则未经过身份验证的用户的所有请求都将被重定向到登录页面或显示 HTTP 403 Forbidden 错误,具体取决于 raise_exception 参数。
就像之前的方法一样,您可以自定义login_url和redirect_field_name
3.基于类的视图方法装饰器
from django.utils.decorators import method_decorator
class ProtectedView(TemplateView):
template_name = 'secret.html'
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
4. HTML 模板
最后,如果您只想为未经身份验证的用户隐藏一些特定的 HTML 块,您可以像这样包装它:
{% if user.is_authenticated %}
<p> Hidden content! </p>
<!-- You can also access the user data like this -->
<p> {{ {{ request.user }} }} </p>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |