3

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

gevorg
  • 4,835
  • 4
  • 35
  • 52
Naoya Makino
  • 557
  • 2
  • 8
  • 24
  • How did you attach bio to the `User` model? It's probably not defined in python, and that's why you're not seeing your intended behavior. Best practice is to use the `UserProfile` paradigm for that sort of thing. See http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users – Sam Dolan Nov 16 '10 at 01:19

2 Answers2

13

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • hi Daniel, it shows up the textarea now!! but one more question. it does not save it to db.. what am I missing to save extra fields to database? thank you so much for your advise! – Naoya Makino Nov 20 '10 at 00:04
1

It is not recommended to directly alter django tables like auth_user although for the above to work, you will have to change the auth model in django.contrib.auth.models to add the bio field.

Recommended way of doing things is via the UserProfile which has a OneToOne relation with the User Table.

digitalPBK
  • 2,859
  • 25
  • 26