8👍
✅
You can set placeholder to form field dynamically:
def someview(request):
form = SomeForm(..)
if some_condition:
form.fields['email'].widget.attrs['placeholder'] = instance.email
👤ndpu
1👍
You can add any form field attributes dynamically during form initialization like this:
class MyForm(forms.Form):
field = forms.TextField()
def __init__(self, *args, **kwargs):
form = super(MyForm, self).__init__(*args, **kwargs)
form.fields['field'].widget.attrs['placeholder']='custom_placeholder'
However, based on your description, are you sure you need placeholders or do you want the form field to be prepopulated with a certain value that will be submitted to the server unless the user changes it.
- [Django]-Why do you need to use Django REST API?
- [Django]-Django : custom management command not registered using app config
- [Django]-Python Vs Ruby On Rails : on Size
Source:stackexchange.com