在 django-templates 中渲染时排除一些表单字段
我有一个代码块,以便在模板中呈现表单字段,这样
{% for field in form.visible_fields %}
<div class="field_container">
<div class="field_label question">
{% field.label_tag %}
</div>
<div class="field_field">
{{ field}}
</div>
{% endfor %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
有没有办法通过指定某些表单字段的名称来排除某些表单字段?
谢谢
I have a code block in order to render the form fields in my template such that
{% for field in form.visible_fields %}
<div class="field_container">
<div class="field_label question">
{% field.label_tag %}
</div>
<div class="field_field">
{{ field}}
</div>
{% endfor %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
Is there any way to exclude some certain form fields by specifying their names ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是模型表单还是常规表单?
如果是 modelForm,
您可以在 modelForm 的 Meta 类上使用 except() 或 fields() 列表。
https: //docs.djangoproject.com/en/1.3/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form
如果是常规的表单:
如果您知道字段的名称,您可以放入一个简单的 if 检查这些字段,如果不是其中之一,则打印您的 html,如果匹配,则不会放置 html。这不是一个理想的解决方案。
更好的方法是创建一个不同的表单字段,其中仅包含您想要的字段并使用该字段。
Is this a modelForm, or a regular form?
If it is a modelForm
you can use the exclude() or fields() list on the Meta class on the modelForm.
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form
If it is a regular form:
If you know the names of the fields you can put in a simple if check for those fields and if it isn't one of them you print your html, if it matches it will not put the html. Not an ideal solution.
The better approach would be to create a different form field that only has the fields that you want and use that one.