Newbie: Saving User and UserProfle from same template

24 views
Skip to first unread message

sarfaraz ahmed

unread,
Aug 1, 2015, 3:58:37 PM8/1/15
to Django users
Hello Team,

I have extending the user with userprofile. I want to change first_name, last_name and sex from userprofile using one template. I have defined form and models. Please hellp this is code for edit_user view, model for user profile and model forms code

-----------------------------------------------------------------------------------------------

class UserProfile(models.Model):
    
    user = models.OneToOneField(User)
    activation_key = models.CharField(max_length=40,blank=True)
    sex = models.CharField(max_length=6, choices=(
        ('male', 'Male'),
        ('female', 'Female'),))
    key_expires = models.DateTimeField(default=datetime.date.today())
      
    def __str__(self):
        return self.user.username

    class Meta:
        verbose_name_plural=u'User profiles'
-----------------------------------------------------------------------------------------------
class UserProfileForm(forms.ModelForm):
    class Meta:
       model=User
       fields =('first_name','last_name')

class UserProfileForm1(forms.ModelForm):
    class Meta:
       model=UserProfile
       fields=('sex',)

----------------------------------------------------------------------------------------------
def edit_user(request):
    args={}
    if request.method=="POST":
        form=UserProfileForm(request.POST, instance=request.user)
        
        if form.is_valid():
            form.save()
        form1=UserProfileForm1(request.POST, instance=request.user)
        if form1.is_valid():
            form1.save()
            UserProfile.save()
            return HttpResponseRedirect('/useraccount/edit_user')
    else:
        form=UserProfileForm()
        form1=UserProfileForm1()
    
    args['form']=form
    args['form1']=form1
    
    return render(request,'useraccount/edit_user.html',args)

Now the i go to edit user view it load blank and do nothing when I click on save. Please help

Regards,
Sarfaraz Ahmed

sarfaraz ahmed

unread,
Aug 2, 2015, 2:55:00 AM8/2/15
to Django users
Changed the edit_user to following. it works fine except... newly added field date_of_birth does not load data.
Please take a look at link below
------------------------------------------------------------------------------------------------------------
Regards,
Sarfaraz Ahmed

sarfaraz ahmed

unread,
Aug 2, 2015, 4:02:08 AM8/2/15
to django...@googlegroups.com
It worked for me....!! Please review my code... and let me know if I have done everything correct...!!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ded7528b-5ca4-46bb-b222-7039892c23b5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Thanks with regards,
Sarfaraz Ahmed


Reply all
Reply to author
Forward
0 new messages