How do I get a Django Server Error 500 report - django

I recently ran my tests on django for my project, one of them turned out as a server error 500. Thi confused me as I thought everything was passing. I currently have DEBUG=True. When I checked the documentation it said to set debug to true and add some admins to email for the full output. Is there an easier way to get the output or should I work on setting that up.
For more info my project is still being developed. I dont really want to post my test code as I really need more debugging experience but if you people ask I will!
Thanks for any help!

Errors are mailed throught Django logging framework. You must configure logging apropiatly to use other Handler than Email (email is by default for errors on django). Change it to Console or File Handler instead....
Here is the source doc:
https://docs.djangoproject.com/en/3.0/topics/logging/

Related

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.

django social auth error after completing pipeline

I am using django social auth to power facebook connect in my app (admittedly with a little complicated user model and legacy database).
I was redirected to error page and seem to be running into an error AFTER completion of pipeline( redirect as last step of pipeline still redirects me).
Can someone tell me where to look to debug this?
Thanks,
A good place to start would be to look in social-auth's views.py, at the few places where the redirect to LOGIN_ERROR_URL happens (the variable url is set to LOGIN_ERROR_URL and then HttpResponseRedirect(url) is called). Add some print statements, or better, set breakpoints using the python debugger. If you run your app in the Django development server, the print statements will show up in the terminal in which you ran the server. Otherwise, they may show up in your server logs, depending on your configuration. You may also find django-debug-toolbar helpful.
Using print statements or the debugger, my workflow would be:
Figure out what line in views.py the redirect is triggered from
Figure out what condition causes that line to be reached
Inspect the variables leading to that condition
Sorry this is so general. Happy to help more if you can provide some more specific information.
Aaron

is it possible to show the debug error page for the admin site only?

sooo with DEBUG = False in your settings.py, it'll show templates/500.html if there's an error etc.
with DEBUG = True, django's development error page thing will show and give you (a developer) I useful error message and debugging info
Q. In a production environment where I have DEBUG = False, is there a way to show the "development error page" for error's which occur in the Django /admin site only?
e.g. it's not neccessary to show a nice looking error page to an admin user, and I could more quickly fix a problem since an admin could just copy and paste and email the error to me, instead of me having to SSH into multiple application server things and inspect the log
Use sentry (which is also available as an open source project that you can host yourself). It will collect all the errors (no matter where they occur), give you a detailed traceback, and notify and track the errors for you.
All that with DEBUG = False.
Django's DEBUG setting does other things (like log every SQL query) which will quickly consume your server memory, but sure why not handle exceptions however you like?
Django's pretty-exception has a lot of code which sanitizes and introspects in django.views.debug but it should be straight forward to spit out a simple exception.
Just create a middleware that uses the process_exception hook.
https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs#process_exception
class MyExceptionHandler(object):
def process_exception(self, request, exception):
if request.path.startswith('/admin/'):
return http.HttpResponse(
"We've encountered an exception in the /admin/ pages!")
You're free to return as much debug information as you'd like.

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/