取消本地化脆皮表格字段(例如纬度和经度)

Unlocalize crispy forms field (such as latitude and longitude)

我希望纬度和经度的值在显示纬度和经度表单字段时始终显示点 (".") 而不是逗号 (",")。 这对于松脆的形式来说似乎很棘手。

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

{% 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 模型有一个经度和纬度字段。

查看 Layout Docs

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

您的代码将有点像这样:

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

希望对您有所帮助。