Skip to content

Instantly share code, notes, and snippets.

@gtindo
Created June 3, 2019 08:09
Show Gist options
  • Save gtindo/2d600f5734b0d791a05a548e8350c506 to your computer and use it in GitHub Desktop.
Save gtindo/2d600f5734b0d791a05a548e8350c506 to your computer and use it in GitHub Desktop.
Django midleware base
from api_v1.models import Notification
class NotificationMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
notifications = Notification.objects.filter(user=request.user)
request.notifications = notifications
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment