4

I have this rather annoying problem:

In the template I have

<div class="form-group">
    {{ form.aboutme.errors }}
    <label for="aboutme">About Me:</label>
    <textarea class="form-control" rows="10" id="aboutme" value="{{form.aboutme}}"></textarea>
</div>

It renders the textarea pre-populated but with the value but prints a redundant "> at the end of the box.

The raw html looks like this:

<textarea class="form-control" rows="10" id="aboutme" value="<p><textarea cols=" 40"="" name="aboutme">My Name is Django</textarea>

Which obviously has an unintnded textarea tag inserted within.

the field is:

 aboutme=models.TextField(blank=True, verbose_name=_('About Me'))

And there is no specia widget used for it in its respective ModelForm class.

So really confused how to correctly render this field?

Paolo
  • 20,112
  • 21
  • 72
  • 113
Jand
  • 2,527
  • 12
  • 36
  • 66

5 Answers5

5

The textarea html tag has no value attribute. The right syntax is:

<textarea class="form-control" rows="10" id="aboutme">{{ form.aboutme.value }}</textarea>
ata843
  • 51
  • 1
  • 3
1

Read more about widgets.

In this situation you could go with following. Try to get the value from the field:

<textarea class="form-control" rows="10" id="aboutme" value="{{form.aboutme.value}}"></textarea>

If it won't work, using article from link above or following SO question add widget to CharField of your form and then simply use:

{{ form.aboutme }}

As I see you've done it and Django successfuly render <textarea> with <p> tags and populated value. You could try to add more cutomization to widget.

Community
  • 1
  • 1
Dracontis
  • 4,184
  • 8
  • 35
  • 50
  • Well, using `value="{{form.aboutme.value }}` removes the extra `">` but caused the form to not be saved. Adding `aboutme = forms.CharField( widget=forms.Textarea )` does not remove the extra `">` issue. – Jand Sep 14 '15 at 07:58
  • @Paulrx , If you use form widget as shown in another answer here or in my linkm then you should replace your html textarea with `{{ form.aboutme }}`. If you're going to stuck with your html, then you should add `name="aboutme"` to the html, so you will be able to save I think. Also, leave another comment next time if you found issues, because I wasn't notify about edits. – Dracontis Sep 14 '15 at 10:08
0
aboutme = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 10, 'cols': 40}))

<div class="form-group">
       <label for="aboutme">About Me:</label>
       {{ form.aboutme.errors }}
</div>
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
0

This no longer works in latest versions. {{form.aboutme}}

Ayub Khan
  • 896
  • 2
  • 14
  • 17
0

While you create an instance of the form in view.py, you can set their initial value by the format below

form = SampleForm()
# CharField
form.fields['title'].initial = name
# TextArea
form.fields['text'].initial = entry

Form class where title is a Textinput and text is a Textarea set through Widgets in django forms

class SampleForm(forms.Form):
    title = forms.CharField(label='Title', widget=forms.TextInput())
    text =  forms.CharField(label='Content', widget=forms.Textarea())

HTML

<form action = '' method = 'post'>
    {% csrf_token %}
    {{form}}
    <button type="submit">Submit</button>
</form>
Aviiciii
  • 1
  • 1