【问题标题】:How to manually make field in HTML? Django如何在 HTML 中手动创建字段?姜戈
【发布时间】:2017-03-12 14:32:01
【问题描述】:

在forms.py中

class PlaceOrder(forms.ModelForm):
    class Meta:
        model = Order
        fields = ["Product_ID","HowMany","DateSubmit",]

调用我常用的表单

{{ Form }}

这将自动呈现表单 但是可以手动制作吗? 例如我希望表单完全像这样

<input id="Product_ID" type="hidden" value="{{ Product.PId }}" >
<input id="HowMany" type="text">
<input id="DateSubmit" type="hidden" value="{{ date }}" >

我试过把它们包起来

{% for field in form %}

但它给出了错误的输出

对不起,如果这令人困惑 但我真的不知道怎么解释 我还是 Django 的新手

【问题讨论】:

标签: python html django django-forms


【解决方案1】:

您应该能够使用类似的方式访问表单中的各个字段

{{Form.Product_ID}}

这将为您提供 Product_ID 字段小部件。

现在,如果您想访问已经预填充的数据,您应该可以使用

{{Form.Product_ID.label}}

或

{{Form.Product_ID.value}}

还要检查: https://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields

【讨论】:

  • Bingo 只是我正在寻找的答案,但我如何指定 {{Form.Product_ID.value}} 的值?谢谢
  • @SamirPrice 欢迎来到stackoverflow,如果此答案解决了您的问题,请标记为正确答案
猜你喜欢
  • 2021-03-10
  • 2021-03-14
  • 1970-01-01
  • 2020-08-17
  • 2015-12-15
  • 2019-07-31
  • 2022-01-24
  • 2018-02-11
  • 2019-09-27
相关资源
最近更新 更多