【问题标题】:Unlocalize crispy forms field (such as latitude and longitude)取消本地化脆表单字段(例如纬度和经度)
【发布时间】:2015-11-02 06:56:26
【问题描述】:

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

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

{% crispy form %}

但我没有在脆皮表格的文档中找到如何做某事。喜欢

{{ value|unlocalize }}

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

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

【问题讨论】:

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


    【解决方案1】:

    查看Layout Docs

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

    你的代码会有点像这样:

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

    希望对你有帮助。

    【讨论】:

    • 好的,这似乎是一种合理的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多