取消本地化脆弱形式字段(例如纬度和经度)

时间:2015-08-10 11:21:29

标签: python django forms localization django-crispy-forms

我想让纬度和经度的值在显示纬度和经度表单字段时始终显示一个点(“。”)而不是逗号(“,”)。 脆皮形式似乎很棘手。

在显示模型字段的模板中,我只使用

{% crispy form %}

但我没有在文件中找到酥脆的表格怎么办......喜欢

{{ value|unlocalize }}

Django documentation提供。由于crispy表单应该是通用的,如下面的代码示例所示,我不知道在哪里设置触发器。

从forms.py中提取

class CrispyForm(ModelForm):
"""
This form serves as a generic form for creating and updating items.
"""
helper = None

    def __init__(self, cancel_button, *args, **kwargs):
        form_action = kwargs.pop('form_action', None)
        model_name = kwargs.pop('model_name', None)
        super(CrispyForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper(self)

        if form_action is not None:
            action = reverse(form_action)
        else:
            action = ""

        # Form attributes
        self.helper.form_method = 'post'
        self.helper.form_action = action
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-10'

        # Save button, having an offset to align with field_class
        save_text = _('Save %(model)s') % {'model': model_name}
        cancel_text = _('Cancel')
        self.helper.layout.append(Submit('save_form', save_text, css_class="btn btn-primary col-sm-offset-2 save_item"))
        self.helper.layout.append(Submit('cancel', cancel_text, css_class="btn btn-primary"))

,这是一个包含经度和纬度模型字段的表单

class SomeItemCreateForm(CrispyForm):
    def __init__(self, *args, **kwargs):
        kwargs['form_action'] = 'create_someitem_url'
        kwargs['model_name'] = self._meta.model._meta.verbose_name
        super(SomeItemCreateForm, self).__init__(False, *args, **kwargs)

    class Meta:
        model = SomeItem
        fields = '__all__'

SomeItem模型具有经度和纬度字段等。

1 个答案:

答案 0 :(得分:1)

查看Layout Docs

您基本上想要为您的字段创建自定义模板,然后使用它。

您的代码有点像这样:

form = SomeItemCreateForm(...)
form.helper.layout = Layout(
    Field('latitude', template='custom_field_template.html'),
    Field('longitude', template='custom_field_template.html')
)

我希望有所帮助。