Skip to main content

Provides Django middleware that detects a country the request came from.

Project description

# Django Country 2

This is a new version of [django-country](https://github.com/color/django-country) for the new versions of Django. A Django middleware that adds current country information to request object. The current country can be set manually or can be detected automatically
based on IP address or locale information in the HTTP header.

## Getting Started

Install django-country package:
```
pip install django-country2
```


Add the middleware in django settings:
```
# settings.py

MIDDLEWARE = [
...
'django_country2.middleware.CountryMiddleware',
]
```
If you use `SessionMiddleware`, it needs to appear before `CountryMiddleware` in the list.

Now your can access detected country as a attribute of `request` object:
```
request.COUNTRY_CODE # Country code is in ISO 3166-1 alpha-2 format. e.g. "US"
```


Add an endpoint to set country manually:
```
# urls.py

urlpatterns = patterns('',
...
url(r'/', include((django_country2.urls')),
)
```
Now you can send POST request to `<host>/country/` with `country` data
to set country manually.


Access country in Django templates:
```
# settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django_country2.context_processors.country',
)

# index.html
<div country-code={{country_code}}>{{country_name}}</div>
```


## How is country detected?

The middleware detects country from the following sources in the given order:

1. HEADER_FORCE_COUNTRY. Name of the heading to force the country code
1. session, if country code is set in session.
1. cookie, if country code is set in cookie.
1. IP address, if `settings.USE_GEOIP` is `True` (default: `False`).
1. HTTP_ACCEPT_LANGUAGE HTTP header if `settings.USE_LOCALE` is `True` (default: `False`).

## Settings

## HEADER_FORCE_COUNTRY
Name of the heading to force the country code

### COUNTRY_CODE
Default: `'US'`

Country code to use if no country is detected.

### COUNTRIES
Default: A tuple of all countries. This list is retrieved from [django-countries](https://github.com/SmileyChris/django-countries) library.

This list is a tuble of two-tuples in the format (country code, country name) - for example ('JP', 'Japan'). This specifies which countries are available for country selection.

### USE_GEOIP
Default: `False`

A boolean that specifieds if IP address should be used to detect country. If this flag is set to `True`, `GEOIP_DAT_PATH` settings has to be specified.

### GEOIP_DAT_PATH
A path to geoip database in .dat format. You can download this from [MaxMind](http://dev.maxmind.com/geoip/legacy/geolite/#Downloads).

### USE_LOCALE
Default: `False`

A boolean that specifieds if locale information in ACCEPT-LANGUAGE HTTP header should be used to detect country.

### COUNTRY_SESSION_KEY
Default: `'django_country2'`

Session key under which the active country for the current session is stored.

### COUNTRY_COOKIE_NAME
Default: `'country'`

Cookie name under which the active country for the current session is stored.

### COUNTRY_COOKIE_AGE
Default: `None` (expires at browser close)

The age of the country cookie, in seconds.

### COUNTRY_COOKIE_PATH
Default: `/`

The path set on the country cookie. This should either match the URL
path of your Django installation or be a parent of that path.

This is useful if you have multiple Django instances running under the
same hostname. They can use different cookie paths and each instance
will only see its own country cookie.

Be cautious when updating this setting on a production site. If you
update this setting to use a deeper path than it previously used,
existing user cookies that have the old path will not be updated. This
will result in site users being unable to switch the country as long as
these cookies persist. The only safe and reliable option to perform the
switch is to change the country cookie name permanently.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-country2-0.0.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_country2-0.0.4-py2-none-any.whl (10.2 kB view details)

Uploaded Python 2

File details

Details for the file django-country2-0.0.4.tar.gz.

File metadata

  • Download URL: django-country2-0.0.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for django-country2-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5d073d0f4242ebf9048371b344c7656f92299857e7944865f7c95813fa1089b5
MD5 17eb5bc98f96060f357783acf776bd79
BLAKE2b-256 7cfe5149ba7628f3a123c3732e6bbae66740e4d34929f5a14d26e91a81d05f5f

See more details on using hashes here.

File details

Details for the file django_country2-0.0.4-py2-none-any.whl.

File metadata

  • Download URL: django_country2-0.0.4-py2-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for django_country2-0.0.4-py2-none-any.whl
Algorithm Hash digest
SHA256 d27f59c35d570faef3f576f3d6e7c9429b6a1ad7e26217b4506fac47007303d2
MD5 6a73fa283df3319199cc545c339a6023
BLAKE2b-256 f15b25b3841439d89460b1c0af8a136febfbf8375fcf2abde4e544847ee67b0f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page