Skip to main content

A django model & fixture containing all data from the countries table of Geonames.org

Project description

https://badge.fury.io/py/django-countries-plus.svg https://travis-ci.org/cordery/django-countries-plus.svg?branch=master https://codecov.io/gh/cordery/django-countries-plus/branch/master/graph/badge.svg

The Country Model

The model provides the following fields (original geonames.org column name in parentheses).

  • iso (ISO)

  • iso3 (ISO3)

  • iso_numeric (ISO-Numeric)

  • fips (fips)

  • name (Country)

  • capital

  • area (Area(in sq km))

  • population (population)

  • continent (continent)

  • tld (tld)

  • currency_code (CurrencyCode)

  • currency_name (CurrencyName)

  • currency_symbol (Not part of the original table)

  • phone (Phone)

  • postal_code_format (Postal Code Format)

  • postal_code_regex (Postal Code Regex)

  • languages (Languages)

  • geonameid (geonameid)

  • neighbors (neighbours)

  • equivalent_fips_code (EquivalentFipsCode)

Installation

Step 1: Install From PyPi

pip install django-countries-plus

Step 2: Add countries_plus to your INSTALLED_APPS

Step 3: Run python manage.py sync (Django <1.7) or python manage.py migrate (Django 1.7+)

Step 4: Load the Countries Data

  1. Load the countries data into your database with the update_countries_plus management command.

    python manage.py update_countries_plus

  2. (alternative) Load the provided fixture from the fixtures directory.

    python manage.py loaddata PATH_TO_COUNTRIES_PLUS/countries_plus/countries_data.json.gz

Usage

Retrieve a Country:

from countries_plus.models import Country
usa = Country.objects.get(iso3='USA')

Update the countries data with the latest geonames.org data:

python manage.py update_countries_data

This management command will download the latest geonames.org countries data and convert it into Country objects. Existing Country objects will be updated if necessary. No Country objects will be deleted, even if that country has ceased to exist.

Add the Request Country to each Request

  1. Add countries_plus.middleware.AddRequestCountryMiddleware to your MIDDLEWARE setting.

  2. add the following two settings to your settings.py:

    COUNTRIES_PLUS_COUNTRY_HEADER - A string defining the name of the meta header that provides the country code. Ex: ‘HTTP_CF_COUNTRY’ (from https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do-)

    COUNTRIES_PLUS_DEFAULT_ISO - A string containing an iso code for the country you want to use as a fallback in the case of a missing or malformed geoip header. Ex: ‘US’ or ‘DE’ or ‘BR’

    Example:

    COUNTRIES_PLUS_COUNTRY_HEADER = 'HTTP_CF_COUNTRY'
    COUNTIRES_PLUS_DEFAULT_ISO = 'US'

Add the Request Country to the Request Context

  1. Enable the optional middleware as described above

  2. Add countries_plus.context_processors.add_request_country to your template TEMPLATE_CONTEXT_PROCESSORS setting (Django <1.8) or to your ‘context_processors’ option in the OPTIONS of a DjangoTemplates backend instead (Django 1.8)

Compatibility

Python 2.7+ & 3.6+. Django 1.11 and Django 2.0+ supported, however should still work on Django 1.4-1.10.

Note: if you are using Django 1.7, tests will fail unless you are using Django 1.7.2 or higher due to a bug in earlier versions.

Integrating with django-languages-plus

If you also have django-languages-plus(https://pypi.python.org/pypi/django-languages-plus) installed then you can run the following command once to associate the two datasets and generate a list of culture codes (pt_BR for example):

from languages_plus.utils import associate_countries_and_languages
associate_countries_and_languages()

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Credits

Tools used in rendering this package:

# Changelog All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.2.0] - 2017-12-13 ### Added - Cleaned up project & improved documentation

### Changed - Added tests for Django 2.0 - Dropped tested support for Django versions < 1.11

## [1.1.0] - 2016-03-14 ### Fixed - Fixed Geonames.org file format error

## [1.0.1] - 2015-06-16 ### Added - Improved test coverage.

### Changed - The Country model has had all fields with undefined lengths (ex: name) expanded to max_length=255. Defined length fields (ex: Iso, Iso3) are unchanged. - Two countries (Dominican Republic and Puerto Rico) have two phone number prefixes instead of 1. These prefixes are now comma separated. - The Country model will now validate on save and reject values of the wrong length. The test suite has been expanded to test this.

### Fixed - Fixed update_countries_plus command for python 3

## [1.0.0] - 2015-06-11 ### Added - Added feature to update data from geonames.org. - Test coverage has been substantially improved.

### Changed - The data migration has been removed in favour of the new management command and manually loading the fixture. - The fixture is no longer named initial_data and so must be loaded manually, if desired. - In order to provide better compatibility with the way Django loads apps the Country model is no longer importable directly from countries_plus. - The get_country_by_request utility function has been moved into the Country model, and is available as Country.get_by_request(request)

### Fixed - General code cleanup & improved test coverage.

### Note - If you have been running an earlier version you should run python manage.py update_countries_plus to update your data tables as they may contain incorrect data.

## [0.3.3] - 2015-01-27 ### Changed - Now uses Django 1.7 data migration pattern

## [0.3.2] - 2015-01-10 ### Fixed - Corrected version number on setup.py

## [0.3.1] - 2015-01-09 ### Added - Now compatible with Python 3 thanks to luiscberrocal

## [0.3.0] - 2014-09-08 ### Added - Now compatible with Django 1.7 thanks to mrben

## [0.2.0] - 2014-02-13 ### Added - Added middleware that adds the request country to the request object.

## [0.1.5] - 2013-06-14 ### Fixed - Corrected model max_length attributes to properly match data.

## [0.1.0] - 2013-05-22 ### Added - Initial release.

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-countries-plus-1.2.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

django_countries_plus-1.2.0-py2.py3-none-any.whl (17.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-countries-plus-1.2.0.tar.gz.

File metadata

File hashes

Hashes for django-countries-plus-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a99dfebb432960945d45d45aa07a8de67aec5d82d88960ca653f2bd6c1b43972
MD5 b6a4fb2856da50368a4b4fec8b3062cd
BLAKE2b-256 d6cbf5858b46204a285e4579f7f74b98702dbda9e4206d86726c36340df1948d

See more details on using hashes here.

File details

Details for the file django_countries_plus-1.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_countries_plus-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4c58cc98ca2fbfa8909058508f1b665f5eaddfd563c41b221e04c76be24d5658
MD5 ce8342ba334b9c8301112c3e110f400d
BLAKE2b-256 1f4b5c9df7ad2e4d93139d82eb332ba1a5d27b0fa32507ab1d35d5e85a1acfa0

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