Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Last active April 2, 2024 16:28
Show Gist options
  • Save kissgyorgy/fe4d148f1f0485258ad0e25f393673cb to your computer and use it in GitHub Desktop.
Save kissgyorgy/fe4d148f1f0485258ad0e25f393673cb to your computer and use it in GitHub Desktop.
Django: Run ASGI application with Werkzeug's DebuggedApplication (traceback on Exception)
import os
from a2wsgi import WSGIMiddleware
from django.core.wsgi import get_wsgi_application
from django.views import debug as django_views_debug
from django_extensions.management.technical_response import (
null_technical_500_response,
)
from werkzeug.debug import DebuggedApplication
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
def make_asgi_app():
django_wsgi_app = get_wsgi_application()
# https://werkzeug.palletsprojects.com/en/3.0.x/debug/
django_debugged_wsgi_app = DebuggedApplication(django_wsgi_app, evalex=True)
django_views_debug.technical_500_response = null_technical_500_response
django_asgi_app = WSGIMiddleware(django_debugged_wsgi_app)
return django_asgi_app
def make_application(http_application):
# To avoid AppRegistryNotReady exceptions
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from core.urls import websockets as ws_urls
return ProtocolTypeRouter(
{
"http": http_application,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(ws_urls))
),
}
)
django_asgi_app = make_asgi_app()
application = make_application(django_asgi_app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment