Reputation: 2727
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?
Upvotes: 4
Views: 9214
Reputation: 1
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>
Upvotes: 0
Reputation: 51
The textarea html tag has no value attribute. The right syntax is:
<textarea class="form-control" rows="10" id="aboutme">{{ form.aboutme.value }}</textarea>
Upvotes: 5
Reputation: 4364
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.
Upvotes: 1
Reputation: 6009
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>
Upvotes: 0