Naoya Makino Naoya Makino - 5 months ago 23
Python Question

How to add extra fields using django.forms Textarea

I am newbie in django and working on a pootle project.

I would like to add bio (in textarea), interests (textarea), and profile pics (image upload). this page is looking like this: http://pootle.locamotion.org/accounts/personal/edit/ (you might need to login to see this page)

I have edited local_apps/pootle_profile/views.py which looks like this:

from django.forms import ModelForm, Textarea

class UserForm(ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'bio')
widgets = {'bio': Textarea(attrs={'cols': 80, 'rows': 20})}


and in ""templates/profiles/edit_personal.html" "

<form method="post" action="/accounts/{{user.username}}/">
<p>
<label for="id_first_name">{% trans 'First Name' %}</label>
{{ form.first_name }}
{{ form.first_name.errors }}
</p>
<p>
<label for="id_last_name">{% trans 'Last Name' %}</label>
{{ form.last_name }}
{{ form.last_name.errors }}
</p>
<p>
<label for="id_email">{% trans 'Email address' %}</label>
{{ form.email }}
{{ form.email.errors }}
</p>
<p>
<label for="id_bio">{% trans 'Bio' %}</label>
{{ form.bio }}
{{ form.bio.errors }}
</p>
<p class="common-buttons-block">
<input type="submit" class="save" value="{% trans 'Save' %}" />
</p>
</form>


but it does not show the last bio textarea field on a profile page.
not loading form.bio. here is html source:

<p>
<label for="id_email">Email address</label>
<input id="id_email" type="text" name="email" value="sample@email.com" maxlength="75" />
</p>
<p>
<label for="id_bio">Bio</label>
</p>


I have added a bio column in auth_user:

MariaDB [pootle]> describe auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(30) | NO | UNI | NULL | |
| first_name | varchar(30) | NO | | NULL | |
| last_name | varchar(30) | NO | | NULL | |
| email | varchar(75) | NO | | NULL | |
| password | varchar(128) | NO | | NULL | |
| is_staff | tinyint(1) | NO | | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
| is_superuser | tinyint(1) | NO | | NULL | |
| last_login | datetime | NO | | NULL | |
| date_joined | datetime | NO | | NULL | |
| bio | text | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)


could somebody see what is missing?
any suggestions are greatly appreciated!!

regards

Answer

Firstly, as Digitalpbk says, don't manually add columns to Django's tables. Instead, create a UserProfile model in your own app, with a OneToOneField to auth.User.

Secondly, to add extra fields to a modelform, you need to define them explicitly at the form level:

class UserForm(ModelForm):                                                    
    bio = forms.CharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 20}))
    class Meta:                                                               
        model = User                                                          
        fields = ('first_name', 'last_name', 'email', 'bio')                  

Finally, you'll need to do something in your view manually to save this extra field, as the User model doesn't know about it.