这可能是一个alreday问的错误,但在我的情况下错误是相同的但不同的问题, 大家好我已经写了一个django apllication,其中我有一套模板
在登录表单中进行身份验证后,用户将转发到dashboards.html。 我的问题是,如果用户直接尝试 dashboards.html 而不通过登录页面,那么我收到此错误。而不是这个我想显示用户login.html如果他试图直接访问dashboards.html。提前谢谢。
我的views.py看起来像
from django.shortcuts import render
from django.contrib.auth.models import User, Group
from django.contrib.auth import authenticate, login, logout
from ow_dashboard.models import Dashboard
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from django.shortcuts import render_to_response
def custom_login(request):
if request.user.is_authenticated():
return HttpResponseRedirect('dashboards')
return login(request, 'login.html', authentication_form=LoginForm)
def custom_logout(request):
return logout(request, next_page='/')
def user(request):
groups = request.user.groups.all()
if request.user.is_anonymous():
groups = []
dashboards = Dashboard.objects.filter(owner=groups)
context = {
'user': request.user,
'groups': groups,
'dashboards': dashboards,
}
return render_to_response('registration/dashboards.html', context, context_instance=RequestContext(request))
# Create your views here.
和dashboards.html就像
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>*OXI-KIBANA*</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
{% block content %}
<div class="container-fluid" style="background-color: #eee; height:60px;">
<div class="row" style="margin-top:1%">
<div class="col-sm-3"><a href="http://www.mpowersoft.com/"><img src="{% static "bootstrap/css/logo4.png" %}" alt="MPOWER"/></a></div>
<div class="col-sm-3"></div>
<div class="col-sm-4" style="padding-right:4%;"><span style="margin-left:125%; ">
{% if user.is_authenticated %}
{{ user.first_name }}
{{ request.user.username }}</span>
{% elseif user.is_anonymous%}
<p>Welcome, new user. Please <a href="login.html">log in</a>.</p>
{% endif %}</div>
<div >
<span style="margin-left:9%;">
<a href="{% url 'django.contrib.auth.views.logout' %}"><button type="button" class="btn btn-xs btn-primary">sign out</button></a></span>
</div>
</div>
<div class="row" style="background-color: #eee; width:19%; height:968%; margin-top:10px;">
<div class="col-sm-2" style="background-color: #eee;">Dashboards
{% for Dashboard in dashboards %}
<strong><a href="#">{{ Dashboard.d_name }}</a><strong>
{% endfor %}
</div>
</div>
<div class="right-contain" style="background-color: #eee; margin-top:-581px; margin-left:18%; width:83%; height:968%;">
kibana
</div>
<div class="row" style="background-color: #eee; margin-top:3px;"><marquee>Footer</marquee>
</div>
</div>
{% endblock %}
</body>
</html>
和urls.py是
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.auth.models import User
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', 'django.contrib.auth.views.login',),
url(r'^logout/$', 'django.contrib.auth.views.logout'),
url(r'^dashboards/$', 'ow_dashboard.views.user'),
)
答案 0 :(得分:0)
你好像不在问你的实际问题。您提到的错误是因为如果用户是匿名的,您似乎将groups
显式设置为空列表。但是你也很清楚你真正想要发生什么,即将用户发送到登录页面。那你为什么不问这个呢?
正如Raja所说, 问题的解决方案是在视图上放置一个@login_required
装饰器。
答案 1 :(得分:0)
是的,和丹尼尔说的一样。你可以运行以下代码来获得结果。
@login_required(login_url='/login/')
def user(request):
groups = request.user.groups.all()
if request.user.is_anonymous():
groups = []
dashboards = Dashboard.objects.filter(owner=groups)
context = {
'user': request.user,
'groups': groups,
'dashboards': dashboards,
}
return render_to_response('test2.html', context, context_instance=RequestContext(request))
&#13;