gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:41:57 25 4
gpt4 key购买 nike

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

我无法在渲染模板时检索偏移值,因此无法分配 margin-left 样式参数。还显示了最终的 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})

我使用 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>

最佳答案

没有必要为此从 CharField 实例化。也许表单中字段的这种初始化对您来说就足够了:

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

关于python - Django表单字段自定义属性分配和在模板中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42317785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com