1

我想动态生成一个表单,并希望分配表单字段的缩进。我正尝试将自定义属性偏移量分配给子类中的forms.CharField。我打算使用这个逻辑从xml文件动态地创建一个表单,其中的字段将根据节点的深度缩进。Django表单字段自定义属性分配和模板中使用

我无法在渲染模板时检索偏移值,因此无法分配页边空白样式参数。最终的html输出也显示出来。

有人可以请帮忙。我在这个网站上搜索了一些其他答案,看起来可以在模板中分配和检索任意属性。例如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}) 

我的模板文件中使用自举看起来是这样的:

{% 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

不需要从CharField实例化那个。可能这种形式的字段初始化对你来说就足够了:

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