Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install emencia.django.countries

How to install emencia.django.countries

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install emencia.django.countries
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
0.1 Available View build log
Linux (32-bit)
0.1 Available View build log
Linux (64-bit)
0.1 Available View build log
 
Author
License
BSD License
Dependencies
Lastest release
version 0.1 on May 6th, 2013

Emencia.django.countries is a Django application who contains world country list, who can be displayed with a priority order.

Usefull for forms or for models which needs a country list ordered.

Installation

You could retrieve the last sources from http://github.com/Fantomas42/emencia-django-countries and running the installation script

$> python setup.py install

or use pip

$> pip install -e git://github.com/Fantomas42/emencia-django-countries.git#egg=emencia.django.countries

In your projects

Register emencia.django.countries in your INSTALLED_APPS section your project settings.

>>> INSTALLED_APPS = (
...   # Your favorites apps
...   'emencia.django.countries',
... )

Now you can run a syncdb for installing the models into your database and the list of all country contained in a fixture.

Usage

By default when the fixture is loaded all the countries a leveled to 0, the default and displayed by alphabetical order.

>>> from emencia.django.countries.models import Country
>>> Country.objects.all()
[<Country: Afghanistan>, <Country: Albania>, <Country: Algeria>, '...(remaining elements truncated)...']

So if you don't want to display Afghanistan as the first country of your list, you have a set a high value to the level attribute for all the countries you want.

Now if you want to retrieve your country list ordered do this :

>>> france = Country.objects.get(iso='FR')
>>> france.level = 100
>>> france.save()

>>> Country.objects.all()
[<Country: France>, <Country: Afghanistan>, <Country: Albania>, '...(remaining elements truncated)...']

But if you only want a short list of countries with the level attribute set, you can do this :

>>> Country.objects.leveled()
[<Country: France>]

This will return only the countries with a level value different than 0.

Template Context Processors

Sometimes it can be usefull to have all the countries in the context for rendering templates, so a template context processor is provided.

>>> TEMPLATE_CONTEXT_PROCESSORS = (
...      # Your template context processors
...      'emencia.django.countries.context_processors.countries',
...   )

This template context processor, provides 2 variables in the context :

  • COUNTRIES_LIST
  • COUNTRIES_LIST_LEVELED

Template tags

But in general it's a waste of ressources to have all the countries loaded in the context, because it make a database request for each request, so template tags have been implemented.

In your templates, to get the countries list, simply do this.

{% load countries_tags %}

{% get_countries %}

It will load the countries list in the local context of the template, in a variable named COUNTRIES But if you want to change the name of this variable use this syntax.

{% get_countries as MY_COUNTRIES %}

If you only want to have the short list of countries, use this tag.

{% get_countries_leveled as MY_COUNTRIES %}

Of course the same syntax apply to this tag.

Changelog

0.1
  • Initial release

Subscribe to package updates

Last updated May 6th, 2013

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.