WSGIRequest对象没有属性'subdomain'

时间:2015-10-04 09:01:02

标签: python django

我正在研究这个项目,顺便说一下,直到几秒钟之前。我在其中一个应用程序视图中添加了一个通用的详细信息视图,它向我发送Template not found error,但我清楚地指出了template_name并确保模板存在。我刷新了页面, BAM 我收到了此服务器错误。

回溯指示了两个文件,以下是代码片段:

general_views.py

def server_error_view(request, template_name='other/500.html', **kwargs):
    return render_to_response(template_name, 4, context_instance=RequestContext(request))

context_processors.py

def applications(request):
    if request.subdomain is not None: #This is where the error occured
        subdomain = str(request.subdomain)+'_'
    else:
        subdomain = ''

和middleware.py

class SubdomainMiddleware:
""" Make the subdomain publicly available to classes """    
def process_request(self, request):
    domain_parts = request.get_host().split('.')
    if (len(domain_parts) > 3):
        subdomain = domain_parts[0]
        if (subdomain.lower() == 'www'):
            subdomain = domain_parts[1]
        else:
            subdomain = None
        domain = '.'.join(domain_parts[2:])
    elif (len(domain_parts) > 2):
        subdomain = domain_parts[0]
        if (subdomain.lower() == 'www'):
            subdomain = None
        domain = '.'.join(domain_parts[1:])
    else:
        subdomain = None
        domain = request.get_host()
    request.subdomain = subdomain
    request.domain = domain

完整的追溯:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/venv/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
  response = self.get_response(request)
  File "/home/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 179, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/home/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/project/hris/views/general_views.py", line 37, in server_error_view
return render_to_response(template_name, 4, context_instance=RequestContext(request))
File "/home/venv/local/lib/python2.7/site-packages/django/template/context.py", line 176, in __init__
self.update(processor(request))
File "/home/project/hris/context_processors.py", line 17, in applications
print request.subdomain
AttributeError: 'WSGIRequest' object has no attribute 'subdomain'

Settings.py

MIDDLEWARE_CLASSES = (
#     'django.middleware.common.CommonMiddleware',
    # tracking
    'tracking.middleware.VisitorTrackingMiddleware',
    'tracking.middleware.VisitorIPTrackingMiddleware',
    # tracking end
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    # my custom middleware:

    'project.middleware.SubdomainMiddleware',


# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# 'django_pdb.middleware.PdbMiddleware',

TEMPLATE_CONTEXT_PROCESSORS = (
    # default context processors
    'django.core.context_processors.request',
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages",
    # custom context processors
    "hris.context_processors.applications",
    "hris.context_processors.organization_context",
)

0 个答案:

没有答案