Forums

unexpected error if DEBUG = False

Hey, I'm going through the django build your first site tutorial.

I was trying to set up 404 handling (as per the link below). My app works like it should according the tutorial, but when I set debug = False, I get "unexpected error" on every page of my site, not just when I try to get a 404. Set it back to True and everything works a-ok. https://docs.djangoproject.com/en/1.3/intro/tutorial03/#raising-404

I read a few people saying to check my logs, but I'm such a noob, I don't know how to do this. I also tried setting the admin in settings to my email, as someone seemed to suggest that I'd get an error report, but no dice there either.

thanks for reading/helping.

if you look at the Web tab on the dashboard, you will see an access log and an error log link. Click on the error log link and page down to the end (this page could be quite lengthy). Check the time where you live and convert to UTC. Look at the error message for approximate time and date. Publish that here.

However, after that lengthy explanation, I think that our problem may be that your ALLOWED_HOSTS in Settings.py looks like this ALLOWED_HOSTS = []. If so, enter a value in the list such as: ALLOWED_HOSTS = ['www.yourwebsite.com'] Seems that when you have DEBUG=True this doesn't matter. When you set it to False for production then it does matter. This is a security feature in Django. Go to Django documentation and search for allowed_hosts.

Caveate: If anyone wants to add to this, please do...

That sounds right -- ALLOWED_HOSTS is the most likely culprit.

Hey, I edited the ALLOWED_HOSTS as prescribed, but I'm still getting the same problem as noted above. Anyway here's the error log I just got:

2013-04-26 17:48:00,873 :Traceback (most recent call last):
2013-04-26 17:48:00,947 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
2013-04-26 17:48:00,947 :    self.load_middleware()
2013-04-26 17:48:00,947 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
2013-04-26 17:48:00,948 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2013-04-26 17:48:00,948 :  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
2013-04-26 17:48:00,948 :    self._setup()
2013-04-26 17:48:00,948 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
2013-04-26 17:48:00,948 :    self._wrapped = Settings(settings_module)
2013-04-26 17:48:00,949 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 90, in __init__
2013-04-26 17:48:00,949 :    mod = importlib.import_module(self.SETTINGS_MODULE)
2013-04-26 17:48:00,950 :  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-04-26 17:48:00,950 :    __import__(name)
2013-04-26 17:48:00,950 :  File "/home/dickdickmore/mysite/settings.py", line 27, in <module>
2013-04-26 17:48:00,950 :    ALLOWED_HOSTS = [dickdickmore.pythonanywhere.com]
2013-04-26 17:48:00,951 :NameError: name 'dickdickmore' is not defined
2013-04-26 17:48:08,873 :Traceback (most recent call last):
2013-04-26 17:48:08,873 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
2013-04-26 17:48:08,873 :    self.load_middleware()
2013-04-26 17:48:08,874 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
2013-04-26 17:48:08,874 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2013-04-26 17:48:08,874 :  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
2013-04-26 17:48:08,874 :    self._setup()
2013-04-26 17:48:08,874 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
2013-04-26 17:48:08,874 :    self._wrapped = Settings(settings_module)
2013-04-26 17:48:08,874 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 90, in __init__
2013-04-26 17:48:08,875 :    mod = importlib.import_module(self.SETTINGS_MODULE)
2013-04-26 17:48:08,875 :  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-04-26 17:48:08,875 :    __import__(name)
2013-04-26 17:48:08,875 :  File "/home/dickdickmore/mysite/settings.py", line 27, in <module>
2013-04-26 17:48:08,876 :    ALLOWED_HOSTS = [dickdickmore.pythonanywhere.com]
2013-04-26 17:48:08,876 :NameError: name 'dickdickmore' is not defined
2013-04-26 17:48:09,114 :Traceback (most recent call last):
2013-04-26 17:48:09,115 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
2013-04-26 17:48:09,115 :    self.load_middleware()
2013-04-26 17:48:09,115 :  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
2013-04-26 17:48:09,115 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2013-04-26 17:48:09,115 :  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
2013-04-26 17:48:09,115 :    self._setup()
2013-04-26 17:48:09,115 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
2013-04-26 17:48:09,116 :    self._wrapped = Settings(settings_module)
2013-04-26 17:48:09,116 :  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 90, in __init__
2013-04-26 17:48:09,116 :    mod = importlib.import_module(self.SETTINGS_MODULE)
2013-04-26 17:48:09,116 :  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-04-26 17:48:09,116 :    __import__(name)
2013-04-26 17:48:09,116 :  File "/home/dickdickmore/mysite/settings.py", line 27, in <module>
2013-04-26 17:48:09,129 :    ALLOWED_HOSTS = [dickdickmore.pythonanywhere.com]
2013-04-26 17:48:09,129 :NameError: name 'dickdickmore' is not defined

[edited by admin for readability]

Hmm, actually I get the same unexpected error's on everything when I have DEBUG = False .... ALLOWED_HOSTS=[dickdickmore.pythonanywhere.com]

I'm ok (besides no 404's) when I put Debug = True and A_H = []

The problem there is that you're not quoting the contents of ALLOWED_HOSTS. It's a list of strings, so you need this:

ALLOWED_HOSTS = ["dickdickmore.pythonanywhere.com"]

That should work.

I made a help page about the django 400 bad request error, hope people find it useful!

Good afternoon. With DEBUG = False, Media files are not displayed

Does this help? https://www.pythonanywhere.com/wiki/DjangoStaticFiles