What is an efficient way to view errors in Django? - django

I am trying to add a new middle ware into my Django application. However, my app now stalls when being visited. Eventually, after a long time, a 500 error surfaces.
Is there an error log anywhere on the server for Django? I want to see what the error was.

In general, logging will depend on how you are serving django (modwsgi, gunicorn, passenger, etc...), how you have configured django logging and where the error occurs, i.e. if the error happens before/after django is handling the request.
If you have configured your mail host and logging, django can send you the error messages. If the error occurs and django isn't handling it, it will generally show in your server's logs.
Personally, I think one of the best ways to view django errors is using raven with sentry, either self hosted or on getsentry. It even comes with a wsgi middleware to catch errors on the wsgi layer.

It looks like you get a timeout error, you can look at your webserver logs but it will probably show nothing interesting, I suppose you are doing something too heavy in your new middleware that takes too long and then times out the request.
I really suggest you to re-check your middleware code (maybe you can add the code here)

Related

Getting 'TooBig' Memcachier exception on a Django Rest Framework response on Heroku

I have a Django site on Heroku, which uses Memcachier's free tier as a basic cache across the whole site. It works fine.
I've just started trying out Django Rest Framework to create a read-only JSON API, and that works fine too, except... one or more API URLs generate 'TooBig' exceptions from Memcachier, even though the response isn't very large (e.g. 20-30 KB):
MemcachedError:
TooBig
No exception message supplied
I'm puzzled as to why this is happening, given the small size, and not sure how to fix it. Maybe I've misunderstood something about how memcached works.
You should increase memcached limits.
See here:
https://django-cachalot.readthedocs.io/en/latest/limits.html#memcached
https://docs.oracle.com/cd/E17952_01/mysql-5.6-en/ha-memcached-cmdline-options.html

Django. How to debug a deployed website without turning on Debug setting

I have a little forum that is already deployed and working. I just found out something is wrong with the Twitter login and I need to debug it. Right now it returns a 500 page. I can't know what is wrong with Debug = True. But if I turn it on, then I'll be violating Django security best practices, and if someone saves the info in the debug page (with Debug=False) and has bad intentions, he could cause lots of trouble. What would you recommend?
Make sure you have access to the application logs and have properly configured the AdminEmailHandler.
Make sure you've property configured ALLOWED_HOSTS. If you haven't configured it and set DEBUG=False you'll see a SuspiciousOperation raised.
Double check that you have the incidentals covered. Bad database connection details will quickly cause 500 errors.
Have you tried running it locally with DEBUG=False? That's often a quick way to find out about some more esoteric errors.
To follow up based on the comments you'll need to turn DEBUG on in a somewhat live environment. The standard way to do this would be to have a separate staging environment from the live one. Since Heroku is being used you can easily spin up a separate environment and set DEBUG=True there. Then you'll see the full error pages, fix the error, and deploy to production where DEBUG=False.
Another idea is to setup a third-party exception handling system like Raygun or Bugsnag. Adding this to the production application will give you reports when exceptions are thrown. This is a big upgrade over Django's default email-on-error behavior.

Django Error Reporting Simple Syntax Errors

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.
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.

Logging Django Errors instead of returning them to inspect API callback

I'm working on a project that gets callbacks from some other sites' API. I expect my code to have a few errors, because I'm new to Python & Django. My site in development is using mod_wsgi and Apache with Debug = True ;)
The API I get the calls from adds a parameter to the querystring to my callback that is built using their own private key. So I have no way of accurately simulating that. If my code fails once I activate my probably faulty key validation code, I have no way of knowing except for apache server logs (which don't show the actual stacktrace or anything).
How can I log a more detailed python exception, like the one I usually see in my browser every 2 minutes ;) to a file? Especially the local variables around the faulty line are interesting obviously :)
Thanks! :)
If you set ADMINS in your settings.py (and set DEBUG=False) you will be emailed all 500 server errors (just like the DEBUG error page).
However, if you want your app to continue without responding with a 500, you can import logging and write your own debug log. There's a good tutorial here:
http://simonwillison.net/2008/May/22/debugging/

Using nginx/fcgi/django, I have form posts that give a 504 gateway time-out

I have an app that uses Django with FCGI on nginx. I'm using the third-party apps like James Bennett's django-registration and django-messages from the Pinax Project. Both of these apps have forms that are submitted and save data into the database, then redirect on to a new URL.
My issue seems to be that the .save() method on any of the forms in these apps cause a 504 gateway time-out when the forms are submitted. All of the data is saved in the database as it should be, however neither seems to ever return anything to the app after the form is saved.
I've done some logging at various points in the code and there are no errors. It's as if the save() methods on the form or the models the forms are connected to simply never return anything--error or otherwise.
With this lack of detail, an answer might be a dream for me, but just a nudge in the right direction or a way to diagnose the issue more completely would be fantastic.
Typically 504's in nginx happen due to timeout between nginx and the fastcgi process. You may want to take a look at your nginx settings and up the fastcgi_read_timeout setting?
Your nginx error log will typically provide a bit more information as to why things are not working as well. If you're on a *nix distro it's typically in "/var/log/nginx/error.log"
Turns out the problem was completely unrelated to nginx, but was Django having a timeout when trying to send an email. Unfortunately, it doesn't drop an error message or any indication that it can't connect to the email server.