如果没有要显示的内容,我想显示空表单字段,否则显示包含值的表单字段:
{% if somevalue %}
{{form.fieldname}} #<---- how do i set the `somevalue` as value of fieldname here?
{% else %}
{{form.fieldname}}
{% endif %}
最佳答案
在你看来这样做,如果它的 CBV
class YourView(FormView):
form_class = YourForm
def get_initial(self):
# call super if needed
return {'fieldname': somevalue}
如果它的通用 View ,与否
FormView
你可以这样做form = YourForm(initial={'fieldname': somevalue})
关于django - 如何设置表单字段值 - django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19479064/