django 'User' object has no attribute 'get' 错误
django 'User' object has no attribute 'get' error
我正在编写一个简单的 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 %}
编辑
这是你的问题,在 url 中,你指向的不是视图,而是 User
。 Python 区分大小写,你知道。 :)
你的问题不是 response
,而是中间件正在获取一个 User
对象,所以在某处,你返回的不是响应而是用户,但我在你的代码中没有看到它。
不管怎样,你为什么不使用 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))
我正在编写一个简单的 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 %}
编辑
这是你的问题,在 url 中,你指向的不是视图,而是 User
。 Python 区分大小写,你知道。 :)
你的问题不是 response
,而是中间件正在获取一个 User
对象,所以在某处,你返回的不是响应而是用户,但我在你的代码中没有看到它。
不管怎样,你为什么不使用 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))