Skip to content Skip to sidebar Skip to footer

How Do I Test A Database Connection In Django?

How do I test a database connection for Django? For example to make sure that the username and password of the database is correct. Or if it is easier, where do I 'catch' the datab

Solution 1:

Write a middleware ("How Django processes a request") which will check for the database connection and redirect to your edit form.

classDbCheckMiddleware(object):

    defprocess_request(self, request):
        try:
            "Some DB Code"
            success = Trueexcept:
            success = False                

        request.db_connection_successful = success

Cache 'db_connection_successful' for performance.

Post a Comment for "How Do I Test A Database Connection In Django?"