为什么我不能在dreamhost上的django应用程序中设置DEBUG = False而不会出现错误?

时间:2010-09-07 12:28:45

标签: django passenger dreamhost

我已成功运行基于django的页面一段时间了。现在我想我已经转向调试模式并实现了一个很好的404.html文件。问题是,只要我在settings.py中将DEBUG更改为'False',我就会立即收到我不理解的服务器错误,无论我尝试访问哪个页面。现有页面或不存在的页面(应该显示我的404.html)都会出错。

我正在使用乘客在dreamhost上运行django。为了获得有用的错误消息,我按照dreamhosts wiki上的说明进行操作,并使用模块粘贴根据these instructions设置我的passenger_wsgi.py文件。这是我的passenger_wsgi.py:

import sys, os
INTERP = "/home/bhogberg/bin/python"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
cwd = os.getcwd()
myapp_directory = cwd + '/homepage'
sys.stdout = sys.stderr
sys.path.insert(0,myapp_directory)
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "homepage.settings"
import django.core.handlers.wsgi
from paste.exceptions.errormiddleware import ErrorMiddleware
application = django.core.handlers.wsgi.WSGIHandler()
# To cut django out of the loop, comment the above application = ... line ,
# and remove "test" from the below function definition.
def testapplication(environ, start_response):
    status = '200 OK'
    output = 'Hello World! Running Python version ' + sys.version + '\n\n'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    #to test paste's error catching prowess, uncomment the following line
    #while this function is the "application"
    #raise("error")
    start_response(status, response_headers)
    return [output]
application = ErrorMiddleware(application, debug=True)

我试图将最后一行的调试更改为False,在这种情况下我也会遇到错误。

只要在settings.py

中有DEBUG = True,一切正常

这是我在打开Debug = False时遇到的错误(通过粘贴报告,而不是django的错误消息):

Server Error

Module paste.exceptions.errormiddleware:144 in __call__
<<              __traceback_supplement__ = Supplement, self, environ
                sr_checker = ResponseStartChecker(start_response)
                app_iter = self.application(environ, sr_checker)
                return self.make_catching_iter(app_iter, environ, sr_checker)
            except:
Module django.core.handlers.wsgi:241 in __call__
<<                  response = http.HttpResponseBadRequest()
                else:
                    response = self.get_response(request)

                    # Apply response middleware
Module django.core.handlers.base:142 in get_response
<<                  exc_info = sys.exc_info()
                    receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
                    return self.handle_uncaught_exception(request, resolver, exc_info)
            finally:
                # Reset URLconf for this thread on the way out for complete
Module django.core.handlers.base:177 in handle_uncaught_exception
<<          mail_admins(subject, message, fail_silently=True)
            # If Http500 handler is not installed, re-raise last exception
            if resolver.urlconf_module is None:
                raise exc_info[1], None, exc_info[2]
            # Return an HttpResponse that displays a friendly error message.

Module django.core.urlresolvers:238 in _get_urlconf_module
<<              return self._urlconf_module
            except AttributeError:
                self._urlconf_module = import_module(self.urlconf_name)
                return self._urlconf_module
        urlconf_module = property(_get_urlconf_module)
Module django.utils.importlib:35 in import_module
<<              level += 1
            name = _resolve_name(name[level:], package, level)
        __import__(name)
        return sys.modules[name]
Module ?:3 in <module>
<<  from django.conf.urls.defaults import *
    from django.contrib import admin
    admin.autodiscover()

    import os.path
Module django.contrib.admin:24 in autodiscover
<<          try:
                before_import_registry = copy.copy(site._registry)
                import_module('%s.admin' % app)
            except:
                # Reset the model registry to the state before the last import as

Module django.utils.importlib:35 in import_module
<<              level += 1
            name = _resolve_name(name[level:], package, level)
        __import__(name)
        return sys.modules[name]
Module ?:6 in <module>
<<  

    admin.site.register(Page, PageAdmin)
Module django.contrib.admin.sites:93 in register
<<  
                # Instantiate the admin class to save in the registry
                self._registry[model] = admin_class(model, self)

        def unregister(self, model_or_iterable):
Module feincms.module.page.models:634 in __init__
<<  
        def __init__(self, *args, **kwargs):
            if len(Page._feincms_templates) > 4:
                del(self.radio_fields['template_key'])
AttributeError: type object 'Page' has no attribute '_feincms_templates'

1 个答案:

答案 0 :(得分:1)

以防万一其他人遇到此问题,这是因为Dreamhost使用的是稍微过时的Django 1.2版本,它有一个错误:

https://code.djangoproject.com/ticket/15343

解决方案是将导入行更改为显式包含handler404和handler500,然后您可以自定义根urlConf。我在下面展示了一个快速示例:

from django.conf.urls.defaults import patterns, include, url, handler404, handler500
from django.conf import settings
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
  url('handler404', 'django.views.defaults.page_not_found'),
  url('handler500', 'django.views.defaults.server_error'),
  url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATICFILES_DIRS[0]}),
  url(r'^$', include('home.urls')),
  url(r'^about/', include('about.urls')),
  url(r'^contact/', include('contactsubmission.urls')),
  url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
  url(r'^admin/', include(admin.site.urls)),
)