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

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.

Related

Django Rest Framework browsable API breaks when changing the port

I'm currently developing my first app with DRF and I'm pretty impressed by it and the browsable API it has build in. However, when I change the port where I host my application (both in manage.py aswell as in the runserver command), all the buttons stop working. I can still click on links to make sure resources where created, but that's about it. I also can't log in and the POST forms don't appear, which is really the most annoying part. Moreover, when I change the port back to the default 8000, it's still broken.
Don't really know if I can post an example of this. It doesn't seem to depend on anything in the actual code. It just happens when I change the port.
So while I don't know what exactly caused it, a CTRL-reload has fixed it for now. Still pops up from time to time.

How do I get a Django Server Error 500 report

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/

FORCE_SCRIPT_NAME is causing urls to resolve incorrectly; can I override this in different contexts?

I'm in the process of upgrading a django app from 1.9.10 to 1.10.5 and am finding that the URL is no longer resolving correctly because FORCE_SCRIPT_URL is set, and is being tacked onto ** everything ** now.
I suspect that this is because of "an addition in django.setup() allows URL resolving that happens outside of the request/response cycle (e.g. in management commands and standalone scripts) to take FORCE_SCRIPT_NAME into account when it is set". (https://docs.djangoproject.com/en/1.10/ref/applications/#django.setup).
If I set the FORCE_SCRIPT_URL, the site works locally on the browser, and I can login, go to different apps within this project, and they all seem to work ok for the most part. However, all of the view tests fail with a 404 error because the URLs are not resolving correctly, and all of my tests involving logging in are also failing (again, I think this is because the url is not resolving correctly). For example, previously, calling reverse(my_url_name) would return /my_path whereas now, it returns /subdomain/my_path.
Are there any work arounds or things I am missing in this upgrade to fix this issue? I'd really prefer not to unset this setting, FORCE_SCRIPT_URL, as there are many things that seem to depend on this.
Thanks in advance!
Simple fix was to just override this setting in our testing environment, but keeping it active in the rest of our settings.

Logging standard out with django and nginx

I am trying to debug an application I made with django and it works fine using django's manage.py, but when I use nginx some features do not work. I checked the error logs and they are empty, probably because I am catching all exceptions and sending them to standard out. Is there an easy way to see the output from the application with nginx?
WSGI servers often disable stdout, I think because it can cause some sort of compatibility problems. Django has built-in logging; I'd suggest using that instead of print statements. It should both be easier to configure and also allow you to have more specificity in your logging, i.e. :
log.debug('only seen while debugging')
or
log.warn('this message might indicate a problem')
Relevant django docs:
https://docs.djangoproject.com/en/dev/topics/logging/

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/