https://github.com/vinitkumar/django-geoip-redirection
GeoIP based redirection middleware for django
https://github.com/vinitkumar/django-geoip-redirection
Last synced: 8 months ago
JSON representation
GeoIP based redirection middleware for django
- Host: GitHub
- URL: https://github.com/vinitkumar/django-geoip-redirection
- Owner: vinitkumar
- License: mit
- Created: 2014-04-26T11:39:44.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-07-05T18:21:12.000Z (almost 12 years ago)
- Last Synced: 2025-02-01T06:11:15.479Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 621 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/vinitkumar/django-geoip-redirection)
Django-geoip-redirection
========================

GeoIP based redirection is awesome and achieving it isn't too hard either. I wrote this middleware
to ensure that my website would get redirected properly with regards to the location of the user
accessing the website.
## Usage:
Install via pypi: `pip install django_geoip_redirection`
Add the middleware in your settings file:
```python
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
'django_geoip_redirection.middleware.LocationMiddleWare',
)
```
Also add `django_geoip_redirection` to INSTALLED APPS:
```python
INSTALLED_APPS = (
'django_geoip_redirection',
....
)
```
## Customization:
```python
# Change the array for extension as per country you
# have to support
if request.path[:4] in ["/en/", "/nl/", "/in/"]:
return None
if 'HTTP_X_FORWARDED_FOR' in request.META:
request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
ip_address = request.META['REMOTE_ADDR']
# get country name using Maxmind database.
# Now, just match and redirect.
# Likewise, replace the name of country to match and redirect.
country = get_country_request(ip_address)
if country == "India":
return HttpResponseRedirect('/in/')
elif country == "Netherlands":
return HttpResponseRedirect('/nl/')
else:
return HttpResponseRedirect('/en/')
return None
```
You would also need to place the `GeoIP.dat.dat` present inside the data folder to
your project root directory.