Skip to main content

Django Country Kit

Django Country Kit is a Django app that provides country-related functionality, including a custom model field for storing country codes, a form widget for selecting countries, and associated tests.

Features

  • Country Model Field: Easily store and retrieve country codes in your Django models.
  • Country Widget: A form widget for rendering a dropdown list of countries in your Django forms.

Installation

  1. Install Django Country Kit using pip:

    pip install django-country-kit
    
  2. Add 'django_country_kit' to your INSTALLED_APPS in your Django project's settings:

    INSTALLED_APPS = [
        # ...
        'django_country_kit',
        # ...
    ]
    
  3. Run collectstatic:

    python manage.py collectstatic
    

Usage

Country Model Field

In your models, use the CountryField to store country codes:

from django.db import models
from django_country_kit.fields import CountryField

class YourModel(models.Model):
    country = CountryField()

For multiple selections:

from django.db import models
from django_country_kit.fields import CountryField

class YourModel(models.Model):
    countries = CountryField(multiple=True)

To exclude or restrict the available countries on a single field (without changing project-wide settings):

from django.db import models
from django_country_kit.fields import CountryField

class YourModel(models.Model):
    # Drop a few countries from the dropdown
    country = CountryField(exclude=["US", "CA"])

    # Or restrict the dropdown to an allow-list
    shipping_country = CountryField(include=["NL", "DE", "FR"])

exclude and include can be combined; exclude always wins.

Country Widget

In your forms, use the CountryWidget to render a dropdown list of countries:

from django import forms
from django_country_kit.widgets import CountryWidget

class YourForm(forms.Form):
    country = forms.CharField(widget=CountryWidget())

For multiple selections:

from django import forms
from django_country_kit.widgets import MultipleCountryWidget

class YourForm(forms.Form):
    countries = forms.CharField(widget=MultipleCountryWidget())

Both widgets accept exclude and include kwargs for per-widget filtering:

from django import forms
from django_country_kit.widgets import CountryWidget

class YourForm(forms.Form):
    country = forms.CharField(widget=CountryWidget(exclude=["US", "CA"]))

Country Class

The Country class represents a country and provides properties for accessing its name and alpha3 code. This class is part of the Django Country Kit and offers convenient functionality for handling country-related data.

Usage

You can create an instance of the Country class to retrieve information about a specific country. Here's how you can use it:

from django_country_kit.base import Country

# Create a Country instance with a specific country code
country = Country(code='US')

# Retrieve the name of the country
country_name = country.name  # Returns 'United States'

# Retrieve the alpha3 code of the country
country_alpha3 = country.alpha3  # Returns 'USA'

Custom settings

Django Country Kit provides custom settings for further customization of country data:

  • OVERRIDE_COUNTRIES: Allows users to override specific countries with custom data.
  • EXCLUDE_COUNTRIES: Allows users to exclude specific countries from the available choices.
  • INCLUDE_COUNTRIES: Allows users to include specific countries in the available choices.

You can customize these settings in your Django project's settings file to tailor the country data according to your needs.

For per-field overrides (rather than project-wide), pass exclude= or include= directly to CountryField, CountryWidget, or MultipleCountryWidget — see the usage examples above.

We appreciate your feedback and contributions to improving Django Country Kit!

Development

If you want to contribute to Django Country Kit, follow these steps to set up your development environment:

  1. Install pipenv if you haven't already:

    pip install pipenv
    
  2. Clone the repository:

    git clone https://github.com/your_username/django-country-kit.git
    
  3. Navigate to the project directory:

    cd django-country-kit
    
  4. Install development dependencies:,

    pipenv install --dev
    
  5. Activate the virtual environment:

    pipenv shell
    
  6. Run migrations:

    python manage.py migrate
    
  7. Run collectstatic:

    python manage.py collectstatic
    
  8. Start the development server:

    python manage.py runserver
    

Metadata

Release files for django-country-kit 0.1.0

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-country-kit 0.1.0
File Size Uploaded Provenance Actions
django_country_kit-0.1.0.tar.gz 18.0 kB Without provenance Details for django_country_kit-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-country-kit 0.1.0
File Interpreter ABI Platform Actions
django_country_kit-0.1.0-py3-none-any.whl Python 3 none any Details for django_country_kit-0.1.0-py3-none-any.whl

Total release size: 36.6 kB

Release files / django_country_kit-0.1.0.tar.gz

Download URL django_country_kit-0.1.0.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c586412af0bf33e138781117ed9ccf43f6d0062ff3a54db2e59699bbdb6607cc
BLAKE2b-256 checksum
How to use checksums
51217128f0ddd5227032e159fc28ba5068b59a028f21a06aab2eebfc56503728
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / django_country_kit-0.1.0-py3-none-any.whl

Download URL django_country_kit-0.1.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a0402ccfb70dbdde12092aad25aa7ba7955496307d5891ae5ab995804fa9f79
BLAKE2b-256 checksum
How to use checksums
72f666ca6235554cf1c4c551bcb7b225b2cd8cf92d5d4d687ed0e29010b62d61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for django-country-kit 0.1.0

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