To get the db name with recent Django versions (1.8+):
from django.db import connection
db_name = connection.settings_dict['NAME']
# Or alternatively
# db_name = connection.get_connection_params()['db']
Be mindful of reading this value after initialization, so that it has the correct value when running unit tests.