Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django edit user profile

I'm trying to create an "Edit Profile" form in the fronted. What happens is that my form(i'm not 100% sure) tries to create a user instead of finding the current user and update his profile. So I think that's the issue. Checked many questions here but none was clear enough. The fields I'm trying to edit are email, first name and last name. (Also I would like to add uda

forms.py

class UpdateProfile(forms.ModelForm):
    username = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)

    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name')

    def clean_email(self):
        username = self.cleaned_data.get('username')
        email = self.cleaned_data.get('email')

        if email and User.objects.filter(email=email).exclude(username=username).count():
            raise forms.ValidationError('This email address is already in use. Please supply a different email address.')
        return email

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']

        if commit:
            user.save()

        return user

views.py

def update_profile(request):
    args = {}

    if request.method == 'POST':
        form = UpdateProfile(request.POST)
        form.actual_user = request.user
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('update_profile_success'))
    else:
        form = UpdateProfile()

    args['form'] = form
    return render(request, 'registration/update_profile.html', args)
like image 892
manosim Avatar asked Mar 21 '14 18:03

manosim


People also ask

How do I update user details in Django REST framework?

Open auth/urls.py and add update profile endpoint. we should send a PUT request to API for checking update profile endpoint. We must add username, first_name, last_name and email. If fields passed validations, user profile will be changed.

How do I change my Django profile picture?

Add django-avatar to the Comp project path. join(PROJECT_DIR, "media") MEDIA_URL = '/media/' INSTALLED_APPS = [ ... 'avatar', ... ] Then run the migrate command to create django-avatar tables: python manage.py migrate .

How can I see profile in Django?

Open users app urls.py and add the route for profile view. Create the template for the view inside the users app template directory. We will modify this template later to display profile of the user, but first there are a couple of things we need to do.

What is UserCreationForm in Django?

Django UserCreationForm is used for creating a new user that can use our web application. It has three fields: username, password1, and password2(which is basically used for password confirmation). To use the UserCreationForm, we need to import it from django. contrib. auth.


2 Answers

You are very close. When you are instantiating the form, you need to pass the User object you are modifying as the instance argument.

From the docs:

A subclass of ModelForm can accept an existing model instance as the keyword argument instance; if this is supplied, save() will update that instance.

In your code, it would look like:

form = UpdateProfile(request.POST, instance=request.user)
if form.is_valid():
    ...

You can checkout more info here: https://docs.djangoproject.com/en/1.6/topics/forms/modelforms/#the-save-method

like image 76
patsweet Avatar answered Oct 21 '22 19:10

patsweet


Or if you wanna go the Class Based View way, using the UpdateView, you could do something like this in views.py:

class UpdateProfile(UpdateView):
    model = MyProfile
    fields = ['first_name', 'last_name', 'image', 'url', 'biography', '...'] # Keep listing whatever fields 
    # the combined UserProfile and User exposes.
    template_name = 'user_update.html'
    slug_field = 'username'
    slug_url_kwarg = 'slug'

where you have something like this for MyProfile in models.py:

class MyProfile(AbstractUser):
    image = models.ImageField(upload_to='uploads/profile/')
    url = models.URLField()
    biography = models.CharField(max_length=1000)

and your urls.py like so (assuming you're using django allauth and wanna honor the url convention):

....
url(r'^accounts/update/(?P<slug>[\-\w]+)/$', views.UpdateProfile.as_view(), name='update_user'),
....

The rest is Django fun! I'll recommend you write less code if you could for basic CRUD tasks, unless its really necessary to do something custom, of which even you still can get away by extending the Class Based Views.

And in case you're in for more, see here: Django Docs on Class Based Views

like image 44
KhoPhi Avatar answered Oct 21 '22 19:10

KhoPhi