Created: 2012-08-21 14:53
Updated: 2014-06-07 21:16


This simple app will add countries list, some templatetags and post handler which will switch country and save that selection in user session.

Country list based on: Country Code List: ISO 3166-1993 (E) This international standard provides a two-letter alphabetic code for representing the names of countries, dependencies, and other areas of special geopolitical interest. The source of this code set is the "Codes for the Representation of Names of Countries (ISO 3166-1993 (E))." Note: 2005-04 correction, Nambia --> Namibia.

Install: Check out this app as 'countries'

Configuration: Add the countries you need to your as well as cookie name (session storage is defult)

COUNTRY_COOKIE_NAME = 'django_country'
    ('AD', gettext('Andorra')),
    ('AE', gettext('United Arab Emirates')),
    ('AF', gettext('Afghanistan')),
    ('AG', gettext('Antigua & Barbuda')),

Next add urls with prefix of yours choice (r'^country/', include('countries.urls')),

Finally enable countries in

	     # 'django.contrib.admindocs',


Create post form:

{% load countries %}	
{% get_current_country as country %}

this will get list of countries specified in or default list if settings are empty
{% get_available_countries as COUNTRIES %}

{% get_country_info_list for COUNTRIES as countries %}
    <form action="/country/setcountry/" method="post" name="countries"> 
    {% csrf_token %}
    <!--- <input name="next" type="hidden" value="/where/to/redirect/next/" /> -->
    <select name="country">
    {% for c in countries %}
    {% ifequal c.code country %}
    <option selected="true" value="{{ c.code }}">{{ }}</option>
    {% else %}
    <option value="{{ c.code }}">{{ }}</option>
    {% endifequal %}
    {% endfor %}
<input type="submit" value="Switch Country"/>

Template tags: get_current_country

 def do_get_current_country(parser, token):
 This will store the current country in the context.

     {% get_current_country as country %}

	 This will fetch the currently active country and
	 put it's value into the ``country`` context


def do_get_country_info_list(parser, token):
	This will store a list of country information dictionaries for the given
	country codes in a context variable. The country codes can be specified
	either as a list of strings or a settings.COUNTRIES style tuple (or any
	sequence of sequences whose first items are country codes).


    {% get_country_info_list for COUNTRIES as countries %}
    {% for c in countries %}
      {{ c.code }}
      {{ }}
      {{ c.name_local }}
    {% endfor %}


def do_get_available_languages(parser, token):
	This will store a list of available countries
	in the context.


	{% get_available_countries as countries %}
    	{% for country in countries %}
    	{% endfor %}

	This will just pull the COUNTRIES setting from
	your setting file (or the default settings) and
	put it into the named variable.


is_eu_country filter will check if current country is in one of 27 member state Usage:: {% get_current_country as country %} {% if country|is_eu_country %} EU country {% else %} NON EU country {% endif %}

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more