Django表单字段自定义属性分配和在模板中使用

时间:2017-02-18 16:36:39

标签: python django twitter-bootstrap django-forms django-templates

我正在尝试动态生成表单,并希望指定表单字段的缩进。我正在尝试将自定义属性 offset 分配给子类中的forms.CharField。我计划使用此逻辑从xml文件动态创建表单,其中字段将根据节点的深度缩进。

渲染模板时无法检索偏移值,因此无法分配margin-left样式参数。最后的html输出也会显示出来。

有人可以帮忙。我在这个网站上搜索了一些其他答案,看起来可以在模板中分配和检索任意属性。 e.g。as in thread here where an arbitrary label_class attribute is assigned

我的forms.py文件:

class MyCharField(forms.CharField):
    def __init__(self, *args, **kwargs):
        self.offset = kwargs.pop('offset', 0)
        super(MyCharField, self).__init__(*args, **kwargs)


class MyDynamicForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(MyDynamicForm, self).__init__(*args, **kwargs)
        self.fields["Field_A"] = MyCharField(label="Input A", offset="5")
        self.fields["Offset_Field_B"] = MyCharField(label="Input B", offset="50")

我的Views.py看起来像这样:

class MyDynamicView(View):
    template_name = 'demo/myform.html'
    form_class = MyDynamicForm

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, {'form': form})

我使用bootstrap的模板文件如下所示:

{% extends 'demo/base.html' %}
{% load bootstrap3 %}
{% block content %}
    <form role="form" method="post">
        {% csrf_token %}
        {% for field in form %}
        <div class="form-group bootstrap3-required">
            <label class="col-md-3 control-label " style = "margin-left: {{field.offset}}px" for="{{ field.name }}">{{ field.label}}</label>
            <div class="col-md-9">
                <input class="form-control" id="id_{{field.name}}" name="{{ field.name }}" placeholder="{{field.label}}" style="margin-left:{{field.offset}}px" title="" required="" type="text"/>
            </div>
        </div>
        {% endfor %}  
        {% buttons submit='OK' reset='Cancel' layout='horizontal' %}{% endbuttons %}
    </form>
{% endblock %}

html输出为:

<form role="form" method="post">
    <input type='hidden' name='csrfmiddlewaretoken' value='lTy0rc2r9KNiNNPosUoriUlNzYBpgoVpael1MYLOczFECO7H7LXdES6EGBhUoXx0' />


    <div class="form-group bootstrap3-required">
        <label class="col-md-3 control-label " style = "margin-left: px" for="Field_A">Input A</label>
        <div class="col-md-9">
            <input class="form-control" id="id_Field_A" name="Field_A" placeholder="Input A" style="margin-left:px" title="" required="" type="text"/>
        </div>
    </div>

    <div class="form-group bootstrap3-required">
        <label class="col-md-3 control-label " style = "margin-left: px" for="Offset_Field_B">Input B</label>
        <div class="col-md-9">
            <input class="form-control" id="id_Offset_Field_B" name="Offset_Field_B" placeholder="Input B" style="margin-left:px" title="" required="" type="text"/>
        </div>
    </div>


    <div class="form-group"><label class="col-md-3 control-label">&#160;</label><div class="col-md-9"><button class="btn btn-default" type="submit">OK</button> <button class="btn btn-default" type="reset">Cancel</button></div></div>
</form>

1 个答案:

答案 0 :(得分:3)

没有必要从CharField实例化。表单中字段的初始化可能就足够了:

  field_a = forms.CharField('Input_A',
                               widget=forms.TextInput(attrs={'placeholder': 'Input_A', 'style': 'margin-left: 50px'}))