Django AttributeError:“NoneType”对象没有属性“has_header”

Posted

技术标签:

【中文标题】Django AttributeError:“NoneType”对象没有属性“has_header”【英文标题】:Django AttributeError: 'NoneType' object has no attribute 'has_header' 【发布时间】:2018-02-11 05:17:00 【问题描述】:

当我尝试登录仪表板时,Django 1.10 出现错误。

Traceback(最近一次调用最后一次):

 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' 对象没有属性 'has_header'

这里是查看代码

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

【问题讨论】:

请出示您的查看代码 我在这里更新了。谢谢 【参考方案1】:

如果profile_obj.role_id != 3,则返回None

    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)

【讨论】:

以上是关于Django AttributeError:“NoneType”对象没有属性“has_header”的主要内容,如果未能解决你的问题,请参考以下文章

Python 报错 AttributeError: module 'django.db.models' has no attribute 'SubfieldBase'(

AttributeError: (Class) object has no attribute '__name__' 创建 ModelForms [Django & Python2.7]

Django 提示:AttributeError : ‘module‘ object has no attribute ‘utcnow‘

Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cli

django-AttributeError: 'str' object has no attribute 'decode'

django-AttributeError: 'str' object has no attribute 'decode'