Package: django-country2
Django Country 2This is a new version ofdjango-countryfor 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 StartedInstall django-country package:pip install django-country2Add the middleware in django settings:# settings.py

MIDDLEWARE = [
    ...
    'django_country2.middleware.CountryMiddleware',
]If you useSessionMiddleware, it needs to appear beforeCountryMiddlewarein the list.Now your can access detected country as a attribute ofrequestobject: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/withcountrydata
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:HEADER_FORCE_COUNTRY. Name of the heading to force the country codesession, if country code is set in session.cookie, if country code is set in cookie.IP address, ifsettings.USE_GEOIPisTrue(default:False).HTTP_ACCEPT_LANGUAGE HTTP header ifsettings.USE_LOCALEisTrue(default:False).SettingsHEADER_FORCE_COUNTRYDefault:NoneName of the heading to force the country codeHEADER_REVERSE_PROXY_COUNTRYDefault:NoneName 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 inCOUNTRIES.COUNTRY_CODEDefault:'US'Country code to use if no country is detected.COUNTRIESDefault: A tuple of all countries. This list is retrieved fromdjango-countrieslibrary.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_GEOIPDefault:FalseA boolean that specifieds if IP address should be used to detect country. If this flag is set toTrue,GEOIP_DAT_PATHsettings has to be specified.GEOIP_DAT_PATHA path to geoip database in .dat format. You can download this fromMaxMind.USE_LOCALEDefault:FalseA boolean that specifieds if locale information in ACCEPT-LANGUAGE HTTP header should be used to detect country.COUNTRY_SESSION_KEYDefault:'django_country2'Session key under which the active country for the current session is stored.COUNTRY_COOKIE_NAMEDefault:'country'Cookie name under which the active country for the current session is stored.COUNTRY_COOKIE_AGEDefault:None(expires at browser close)The age of the country cookie, in seconds.COUNTRY_COOKIE_PATHDefault:/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.