Question

I'm brand new to Django and am working through the tutorial. I fat-fingered a bit of code which would normally raise a syntax error or name error if I were running the file like a standard script. However, when I try to browse to the view in my web-browser using manage.py runserver I just get a blank page with no relevant error/traceback. The same is true of the console from which I ran manage.py runserver -- it just shows the GET request. Also, when starting the server, the console displayed "0 errors found" which was clearly false.

My question is how does Django report errors and tracebacks? Do I need to enable anything? (I have DEBUG = True in my settings.py file) It's kind of un-nerving to me that I could make a little typo and all of a sudden the entire website stops working and I'm not given a report of where the problem is.

Was it helpful?

Solution

Regarding running the development server, it'll say that there are 0 errors found in getting the server started. This typically refers to configuration and syntax in your settings.py file and models. It will only report errors that come up through the startup code path, which typically it doesn't go through your view handlers.

As you modify files, the development server is smart enough to reload itself. Syntax errors will crash the development server.

If you are seeing a blank page and the GET request returning successfully, then your code is working fine from a Django perspective. It's handling the request and returning a response, though you know the response is incorrect. Typically, syntax errors here will cause the view to return a 500, where you'll see debug information returned in the view (if DEBUG = True) and you'll see it in the console as well. It would seem that your typo was valid python as it went through your view handler successfully to return a blank page. There really isn't anything you can do here other than unit tests as to Django, a blank page could be a valid response.

Now as you move from development to production, you'll likely want to know when a page runs into an error. Django builds off Python's logging framework to handle error and other messages. The defaults in your settings.py file, set up a mail_admins handler that handlers error on django.request. If you set up ADMINS and EMAIL_BACKEND in your settings file, you'll get e-mailed the stack trace for any 500 error that happens on your server. As you get more sophistication, you can create your own logging by sending messages to Python's logging framework and custom handlers.

import logging

logger = logging.GetLogger(__name__) #using the module name so in your settings.py you can configure different settings per module

logger.info('msg')
logger.error('msg')

I'd recommend looking into Sentry as a handler for your error messages, that does hit counting and stores your error logs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top