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

前端 未结 1 466
生来不讨喜
生来不讨喜 2021-01-22 07:40

I am writing an simple django application and got stuck into this error, can some one please help me my views.py looks exactly as

相关标签:
1条回答
  • 2021-01-22 07:52

    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))
    
    0 讨论(0)
提交回复
热议问题