Search code examples
djangoservercrashrestart

What happen in production mode if Django crash?


I'm new to Django. Since in a development environment it happens that an error occurs after a request (often because it is badly handled) and Django crashes, I thought about creating a script (in production) to detect if Django is running and if not run it again (something like this). And of course after adding block try except everywhere.

But since Django is well done it wouldn't surprise me if this concern is already taken in consideration, hence my question.

Thank you.


Solution

  • In a production environment, you have to use debug=False mode in the settings.py. A few moments since you've set up debug false mode:

    • you need to serve your static files, so you need NGINX or Apache. Alternatively, you can use CDN for the static files (AWS, Digital Ocean, any other popular options)
    • you need to run Django by a web server behind reverse-proxy. There are several choices, I prefer Gunicorn + NGINX
    • Any unhandled exception that blocks request-response will raise 500 Internal Server Error for a user docs
    • you can override 500 error template (create 500.html in your root template directory)
    • Django will continue handling other requests normally.

    Additional:

    • it is good practice to log such cases, use any cloud-based error monitoring, ex. sentry

    More info about Django deployment is here