Display content based on location with Django -
how display content users based on location in?
for example if display blog section users in canada , not users in usa. how django best practice?
i have not found explains how works.
you can use ipinfodb's api location , use in django view. example:
import requests # pip install requests def myview(request): x_forwarded_for = request.meta.get('http_x_forwarded_for') if x_forwarded_for: ipaddress = x_forwarded_for.split(',')[-1].strip() else: ipaddress = request.meta.get('remote_addr') ipinfodb_data = requests.get('http://api.ipinfodb.com/v3/ip-country/?key=<your_key_from_ipinfodb>&ip={0}'.format(ipaddress)) country = ipinfodb.text.split(':')[4] context = dict() if country != 'united states': context['blog'] = blog.objects.all() # other stuff in context return render_to_response('my_template.html', context=context)
and template should this:
{% if blog %} {% b in blog %} {{ b.title }}: {{ b.author }} {% endfor %} {% else %} <!-- stuff --> {% endif %}
Comments
Post a Comment