我正在编写一个简单的django应用程序,并且遇到了这个错误,有人可以帮助我 我的views.py看起来完全像
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):
context = {'user': user, 'groups': request.user.groups.all(), 'dashboards': Dashboard}
return render_to_response('registration/dashboards.html', context, context_instance=RequestContext(request))
和我的forms.py就像
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
#from mhawk.models import Dashboard
class LoginForm(forms.Form):
username = forms.CharField(label=(u'User Name'))
password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
问题是我试图在html页面中显示登录的用户名。
这是我追溯错误
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/user/
Django Version: 1.7.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mhawk')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
204. response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py" in process_response
31. if response.get('X-Frame-Options', None) is not None:
Exception Type: AttributeError at /user/
Exception Value: 'User' object has no attribute 'get'
urls.py看起来像
from django.conf.urls import patterns, include, url
from django.contrib import admin
from drinker import views, models
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'^user/$', 'mhawk.views.User'),
)
dashboards.html: -
{% extends "base.html" %}
{% block content %}
{% if user.is_authenticated %}
<p>Welcome, {{ request.user.get_username }}. <br/>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
<ul>
{% for group in groups %}
<li>
<strong>{{ group.name }}<strong> -
{{ dashboards.d_name }}{% if not forloop.last %},{% endif %}
</li>
{% endfor %}
</ul>
{% endblock %}
答案 0 :(得分:1)
修改
您的问题是,在网址中,您指的不是视图,而是指向User
。 Python区分大小写,您知道。 :)
你的问题不是response
,而是中间件正在获得一个User
对象,所以somwhere,而不是你回复用户的响应,我在你的代码中看不到它。
无论如何,为什么不使用django的身份验证视图?他们做的就像你试图实现的那样。
from django.contrib.auth.views import login, logout
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='/')
哦,是的,并将其添加到您的设置中:
LOGIN_REDIRECT_URL = '/dashboards/'
这是一个承诺的user
观点:
from django.contrib.auth.decorators import login_required
@login_required
def user(request):
# btw 'user' variable is already available in templates
context = {'user': request.user}
return render_to_response('dashboards.html', context, context_instance=RequestContext(request))