Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add default value to Django form TextArea?

class Meta:

    model = LectureSession
        fields = ['elf', 'elf_notes']
        widgets = {
        'elf_notes': Textarea(attrs={'cols': 80, 'rows': 40}), 
    }

I want to add default text to the elf_notes text area.

like image 788
Ally McKnight Avatar asked May 22 '16 23:05

Ally McKnight


1 Answers

There are multiple ways of doing it, you can pass initial data when you initialize your form:

initial = {'elf_notes': 'This is default text.'}
form = MyForm(initial=initial)

Or you can set it after initialization, something like this:

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['elf_notes'].initial = 'This is default text.'
like image 195
Aamir Rind Avatar answered Oct 20 '22 06:10

Aamir Rind