Skip to content

Instantly share code, notes, and snippets.

@phalt
Last active January 3, 2016 08:09
Show Gist options
  • Save phalt/8433810 to your computer and use it in GitHub Desktop.
Save phalt/8433810 to your computer and use it in GitHub Desktop.
Useful decorator around the django.contrib.sites package for view functions.
from django.conf import settings
from functools import wraps
from django.contrib.sites.models import get_current_site
from django.http import HttpResponseNotFound
def main_site_only(function):
"Use this decorator on views that should only be visible on your main site"
@wraps(function)
def decorator(request, *args, **kwargs):
if get_current_site(request).id in settings.SITE_ID:
return function(request, *args, **kwargs)
else:
# Change this to a TemplateResponse if you want to return a template
return HttpResponseNotFound()
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment