简体   繁体   English

django localflavor国际站点

[英]django localflavor for international site

Looks like the general usage of localflavor is import the country specific package: 看起来localflavor的一般用法是导入特定于国家/地区的软件包:

from localflavor.nz.forms import NZRegionSelect

What if I have a site that supports multiple countries? 如果我的网站支持多个国家怎么办? Is there generic proxy to be country agnostic, something like: 是否有与国家无关的通用代理,例如:

from localflavor.autodetect.forms import RegionSelect

__import__ would do the trick: __import__可以解决问题:

def get_region_select(country_code):
    module_path = 'django.contrib.localflavor.{}'.format(country_code)
    try:
        module = __import__(module_path, fromlist=['forms'])
    except ImportError:
        return None

    fieldname = '{}RegionSelect'.format(country_code.upper())
    if hasattr(module.forms, fieldname):
        return getattr(module.forms, fieldname)()
    return None

Adapted from: http://codeinthehole.com/writing/validating-international-postcodes-in-django/ 改编自: http : //codeinthehole.com/writing/validating-international-postcodes-in-django/

Then, in your template you'll have to reload the page each time you change the country, and do something like this in the view: 然后,在模板中,您每次更改国家/地区时都必须重新加载页面,并在视图中执行以下操作:

form.fields['region'].widget = get_region_select(country)

Since different regions will have different choices. 由于不同的地区会有不同的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM