Skip to content

Instantly share code, notes, and snippets.



Last active Jul 1, 2020
What would you like to do?
Adding custom header with the deploy datetime of a Django app. Useful also for adding revision, and other deploy information
import os
import subprocess
from datetime import datetime
import django
from django.core.handlers.wsgi import WSGIHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings.develop")
DEPLOY_REVISION = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
class Application(WSGIHandler):
def __init__(self):
deploy_datetime = datetime.utcnow().isoformat()
self.served_by = f"revision={DEPLOY_REVISION}&date={deploy_datetime}"
def get_response(self, request):
setattr(django, "_wsgi_get_response", True) # noqa: B010
response = super().get_response(request)
response["X-Served-By"] = self.served_by
return response
def get_wsgi_application():
return Application()
application = get_wsgi_application()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment