Skip to content

Instantly share code, notes, and snippets.

@anhtran
Created August 16, 2013 22:08
Show Gist options
  • Save anhtran/6253890 to your computer and use it in GitHub Desktop.
Save anhtran/6253890 to your computer and use it in GitHub Desktop.
Django template tags: check the request agent is Facebook.
from django import template
register = template.Library()
@register.assignment_tag(takes_context=True)
def is_facebook(context):
request = context.get('request')
a = request.META['HTTP_USER_AGENT']
f = ['facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)',
'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)']
if a in f:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment