Skip to main content

Django Country 2

This is a new version of 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
  2. session, if country code is set in session.
  3. cookie, if country code is set in cookie.
  4. IP address, if settings.USE_GEOIP is True (default: False).
  5. HTTP_ACCEPT_LANGUAGE HTTP header if settings.USE_LOCALE is True (default: False).

Settings

HEADER_FORCE_COUNTRY

Default: None

Name of the heading to force the country code

HEADER_REVERSE_PROXY_COUNTRY

Default: None

Name of the header that forwards the detected country code. Useful for use with reverse proxies such as CloudFlare. Set this to CF-IPCountry to use the country code detected by CloudFlare. Remember that by default the XX or T1 countries are invalid and will be ignored unless they are added to the list of countries in COUNTRIES.

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 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.

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.

Default: 'country'

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

Default: None (expires at browser close)

The age of the country cookie, in seconds.

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.

Metadata

Release files for django-country2 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-country2 0.0.8
File Size Uploaded Provenance
django-country2-0.0.8.tar.gz 5.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-country2 0.0.8
File Interpreter ABI Platform
django_country2-0.0.8-py2-none-any.whl Python 2 none any Details

Total release size: 15.8 kB

Release files / django-country2-0.0.8.tar.gz

Download URL django-country2-0.0.8.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a5c84e6ae17856bc7f6d00b7fd2e3d893b927161d02745658246a076110abbe5
BLAKE2b-256 checksum
How to use checksums
10fef415c9a9c534dfa30a99c483c8cb025237cba705d2135bbbae15f81c74dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.2

Release files / django_country2-0.0.8-py2-none-any.whl

Download URL django_country2-0.0.8-py2-none-any.whl
Size 10.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
700f7e4b40481b31cbb332409585bbe8c95b284f9f518f160a170077c6be1675
BLAKE2b-256 checksum
How to use checksums
1f6339f9eb83e475b6b9ef07eec9ec0af1a95a07394acb4acd732af65ac75cf0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.2

Security for django-country2 0.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page