Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django AttributeError: 'NoneType' object has no attribute 'has_header'

Django 1.10 give error when i am try to login in my dashboard.

Traceback (most recent call last):

 File "/Users/guest/epifiction/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)

 File "/Users/guest/epifiction/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)

 File "/Users/guest/epifiction/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)

 File "/Users/guest/epifiction/lib/python2.7/site-packages/django/views/decorators/cache.py", line 58, in _wrapped_view_func
add_never_cache_headers(response)

 File "/Users/guest/epifiction/lib/python2.7/site-packages/django/utils/cache.py", line 230, in add_never_cache_headers
patch_response_headers(response, cache_timeout=-1)

  File "/Users/guest/epifiction/lib/python2.7/site-packages/django/utils/cache.py", line 219, in patch_response_headers
if not response.has_header('Last-Modified'):

AttributeError: 'NoneType' object has no attribute 'has_header'

Here is View code

class AuthorLoginView(bracesviews.AnonymousRequiredMixin,authviews.LoginView):
   form_class = forms.AuthorLoginForm
   template_name = 'landing_page/demo.html'

 def form_valid(self, form):
    username = form.cleaned_data.get('username')
    userId = User.objects.get(username=username).id
    try:
        profile_obj = UserProfile.objects.get(user_id=userId)
    except UserProfile.DoesNotExist:
        return super(AuthorLoginView, self).get(self)
    else:
        if profile_obj.role_id == 3:
            redirect = super(AuthorLoginView, self).form_valid(form)
            remember_me = form.cleaned_data.get('remember_me')
            if remember_me is True:
                ONE_MONTH = 30 * 24 * 60 * 60
                expiry = getattr(settings, "KEEP_LOGGED_DURATION", ONE_MONTH)
                self.request.session.set_expiry(expiry)
            return redirect
like image 444
user2908424 Avatar asked Sep 02 '17 08:09

user2908424


2 Answers

You return None if profile_obj.role_id != 3

    if profile_obj.role_id == 3:
        redirect = super(AuthorLoginView, self).form_valid(form)
        remember_me = form.cleaned_data.get('remember_me')
        if remember_me is True:
            ONE_MONTH = 30 * 24 * 60 * 60
            expiry = getattr(settings, "KEEP_LOGGED_DURATION", ONE_MONTH)
            self.request.session.set_expiry(expiry)
        return redirect
    # HERE need add logic simple return super
    return super(AuthorLoginView, self).form_valid(form)
like image 83
Brown Bear Avatar answered Oct 24 '22 09:10

Brown Bear


sometimes you forgot to return something, this error can be caused by a missing 'return' ex:

 Response(data=response.json(),status=response.status_code)

don't forget to always return something, ex

return Response(data=response.json(),status=response.status_code)
like image 1
Aspc Starossa Avatar answered Oct 24 '22 08:10

Aspc Starossa