Django - 如何在视图中更改窗体的占位符

时间:2018-04-30 04:45:48

标签: django placeholder

如何在视图中更改表单的占位符?

def myview(request):
    form = MyForm()
    form.widget.attrs.placeholder = 'Replace placeholder value'

1 个答案:

答案 0 :(得分:0)

您可以动态设置占位符到表单字段:

def someview(request):
    form = SomeForm(..)

    if some_condition:
        form.fields['yourfield'].widget.attrs['placeholder'] = instance.yourfield