Django:如何添加动态类以选择表单中的选项 [英] Django: How to add dynamic classes to select options in a form

查看:350
本文介绍了Django:如何添加动态类以选择表单中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用django-widget-tweaks在模板中呈现的ModelForm.我有一个ChoiceField选择,它可以打印出以下内容:

I have a ModelForm that I'm rendering in the template with django-widget-tweaks. I have a ChoiceField select that prints out something like this:

<select>
    <option value="object.id">object.name</option>
</select>

但是出于Javascript操作的目的,我需要以下内容:

But for Javascript manipulation purposes, I need the following:

<select>
    <option value="object.id" class="object.otherPropery">object.name</option>
</select>

django-widget-tweaks具有add_class函数,但只会将一个类添加到选择框,而不是所包含的选项.

django-widget-tweaks has an add_class function, but it only adds a class to the select box, not the contained options.

我也尝试过滚动自己的选择框:

I have also tried rolling my own select box:

<select>
    {% for object in form.object_field %}
        <option value="{{ object.id }}" class="{{ object.otherProperty }}">{{ object.name }}</option>
    {% endfor %}
</select>

但是,当表单无法通过验证并返回错误时,用户先前选择的选项将重置为默认(第一个)选项(此确实发生在django-widget-tweaks中)

But when the form does not validate and errors are returned, the option the user had selected previously gets reset back to the default (first) option (this does happen with django-widget-tweaks).

我也尝试覆盖Select小部件的render_optionrender_options字段,但是对象的其他属性没有传递给这些函数.

I have also tried overriding the render_option and render_options fields of the Select widget, but the additional properties of the object are not being passed to those functions.

我觉得我想做的事情有一个简单的解决方案.任何帮助将非常感激!

I feel like there's a simple solution for what I'm trying to do. Any help would be much appreciated!

推荐答案

我猜你可以做类似的事情(注意'if'标签):

I guess you can do something like this (note the 'if' tag):

<select>
    {% with value=form.object_field.value %}
        {% for object in form.object_field %}
            <option 
                value="{{ object.id }}" 
                class="{{ object.otherProperty }}"
                {% if object.id == value %}selected="selected"{% endif %}
            >
                {{ object.name }}
            </option>
       {% endfor %}
    {% endwith %}
</select>

尽管if条件可能不会在应有的情况下得出正确的结果,但如果object.id是整数,因为value始终是已提交表单的字符串(POST/GET中的所有值)查询数据是字符串).但是,如果该值来自"initial_data",则该值将为整数.

Although the if condition might not result in a true result when it should, if the object.id is a integer because the value would always be a string for a already submitted form (all the values in the POST/GET query data are strings). But if the value comes from the 'initial_data' it would be integer.

在这种情况下,您可以按以下方式更新"if"标签:

For such cases you may update your 'if' tag as follows:

{% if object.id|slugify == value|slugiy %}selected="selected"{% endif %}

按如下所示制作最终代码:

Making the final code as follows:

<select>
    {% with value=form.object_field.value %}
        {% for object in form.object_field %}
            <option 
                value="{{ object.id }}" 
                class="{{ object.otherProperty }}"
                {% if object.id|slugify == value|slugify %}selected="selected"{% endif %}
            >
                {{ object.name }}
            </option>
       {% endfor %}
    {% endwith %}
</select>

这篇关于Django:如何添加动态类以选择表单中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆