Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could cause a Django error when debug=False that isn't there when debug=True

Using the development server, it works with debug=True or False.

In production, everything works if debug=True, but if debug=False, I get a 500 error and the apache logs end with an import error: "ImportError: cannot import name Project".

Nothing in the import does anything conditional on debug - the only code that does is whether the development server should serve static files or not (in production, apache should handle this - and this is tested separately and works fine).

like image 898
willcritchlow Avatar asked Feb 11 '11 15:02

willcritchlow


People also ask

What does debug false do Django?

When DEBUG is False , Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (strictly speaking, for any response with an HTTP status code of 500 or greater). This gives the administrators immediate notification of any errors.

What does Django debug true do?

One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).

How do you change the Django settings debug to false?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.

What does debug false mean in Python?

What does DEBUG false mean in Python? debug=true is for debugging during development. It creates debugging symbols used to provide metadata about the current executing code. debug=false is is for deployment to a production server.13-May-2015.


2 Answers

Just to say, I ran into a similar error today and it's because Django 1.5 requires the ALLOWED_HOSTS parameter in the settings. You simply need to place this row to make it work ;)

...
ALLOWED_HOSTS = '*'
...

However, be aware that you need to set this parameter properly according to your actual host(s) (https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts)!

Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).

So basically it's better for you to use this type of configuration once you're in production:

...
ALLOWED_HOSTS = [
    '.yourdomain.com',
]
...

thanks to gertvdijk for pointing this out

like image 112
VAShhh Avatar answered Oct 13 '22 18:10

VAShhh


This happens if you have a circular import in one of your files. Check and see if you are importing something from Project and then importing something in Project from the original file that originally imported Project.

I ran into this same problem recently, and rearranging some of my imports helped fix the problem.

like image 22
Ben Belchak Avatar answered Oct 13 '22 20:10

Ben Belchak