Saturday, August 1, 2015

Newbie: Saving User and UserProfle from same template

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/74aaaff7-eec5-4564-a6e3-36c325b8e859%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment