django 'User' object has no attribute 'get' error

此生再无相见时 提交于 2019-12-02 01:24:03

Edit

There is your problem, in urls, you are pointing not to a view, but to a User. Python is case sensitive, you know. :)


Your problem is instead of response, middleware is getting a User object, so somwhere, instead of response you are returning a user, I don't see it in your code though.

Regardless, why don't you use django's authentication views? They do same stuff as you are trying to implement.

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='/')

Oh yeah, and add this to your settings:

LOGIN_REDIRECT_URL = '/dashboards/'

And here is a promised user view:

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))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!