Python Django表单字段自定义属性分配和在模板中使用
我正在尝试动态生成表单,并希望分配表单字段的缩进。我正在尝试为子类中的forms.CharField分配一个自定义属性offset。我计划使用此逻辑从xml文件动态创建表单,其中字段将根据节点的深度缩进 渲染模板时无法检索偏移量的值,因此无法指定边距左侧样式参数。最后的html输出也显示出来 有人能帮忙吗。我在这个网站上搜索了一些其他的答案,似乎可以在模板中分配和检索任意属性。e、 g My forms.py文件:Python Django表单字段自定义属性分配和在模板中使用,python,django,twitter-bootstrap,django-forms,django-templates,Python,Django,Twitter Bootstrap,Django Forms,Django Templates,我正在尝试动态生成表单,并希望分配表单字段的缩进。我正在尝试为子类中的forms.CharField分配一个自定义属性offset。我计划使用此逻辑从xml文件动态创建表单,其中字段将根据节点的深度缩进 渲染模板时无法检索偏移量的值,因此无法指定边距左侧样式参数。最后的html输出也显示出来 有人能帮忙吗。我在这个网站上搜索了一些其他的答案,似乎可以在模板中分配和检索任意属性。e、 g My forms.py文件: class MyCharField(forms.CharField):
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")
My 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 %}
使用引导的我的模板文件如下所示:
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 %}
{%extends'demo/base.html%}
{%loadbootstrap3%}
{%block content%}
{%csrf_令牌%}
{%形式的字段为%}
{{field.label}
{%endfor%}
{%buttons submit='OK'reset='Cancel'layout='horizontal%}{%endbuttons%}
{%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"> </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>
输入A
输入B
好的,取消
没有必要为此从CharField实例化。对表单中的字段进行这样的初始化可能就足够了:
field_a = forms.CharField('Input_A',
widget=forms.TextInput(attrs={'placeholder': 'Input_A', 'style': 'margin-left: 50px'}))