2

I am setting up a website in Django, where users can see their profiles and update details (like email, password, etc) or delete own account. But updating the form is not acknowledged at all by the user. I am using the standard built-in User model.

forms.py:

class UserDetailsForm(forms.ModelForm):
    password = forms.CharField(widget = forms.PasswordInput())
class Meta:
    model = User
    fields = ('first_name','last_name','email','password','is_active')

views.py:

@login_required
def edit_profile(request):
    user = User.objects.get(username=request.user)
    form = UserDetailsForm(request.POST or None,
                       initial={'first_name':user.first_name,
                                'last_name':user.last_name,
                                'email':user.email,
                                'password':user.password,
                                'is_active':user.is_active,})
    if request.method == 'POST':
        if form.is_valid():
            user.save()
            messages.info(request, 'This is a debug message')
            return HttpResponseRedirect(reverse('account'))

    context = {"form": form}

return render(request, "iFood/user-account.html", context)    

user-profile.html:

...
<form method="POST" action="{% url 'account' %}" class="" enctype="multipart/form-data">
{% csrf_token %}
        {{form.as_p}}
        <input type="submit" name="" value="Edit and Save">
            {% if messages %}
            <ul class="messages">
                {% for message in messages %}
                <li class="{{ message.tags }}">
                    {{ message }}
                </li>
                {% endfor %}
            </ul>
            {% endif %}

Mario Boss
  • 1,784
  • 3
  • 20
  • 43
Zuz
  • 57
  • 4
  • You never associate the `user` with the `form`, and you never save the data that's submitted in the form, so why would you expect something to happen? `user.save()` just saves the user object, unchanged. Read [this](https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#the-save-method) to see how to specify the instance for the form and to save it. – dirkgroten Mar 04 '19 at 17:55
  • Also note that you cannot display and change a user's password this way. First, you don't have the user's password (luckily!) if you're using Django standard auth. Second, if the user changes the password, you need to save it as a hash using the `set_password()` method on the `User` model. – dirkgroten Mar 04 '19 at 17:59

1 Answers1

1

First of all you can't change a password like that. You should take the password that user entered and set it with user_obj.set_password():

Django docs: Change password

And for your form:

You doing it wrong with user.save(). There is nothing to save for user object. You should save the form using form.save().

Also request.user is the actual user object not the username.

forms.py:

class UserDetailsForm(forms.ModelForm):
    password = forms.CharField(widget = forms.PasswordInput())
    class Meta:
        model = User
        fields = ('first_name','last_name','email'','is_active')

views.py:

@login_required
def edit_profile(request):
    user = request.user
    form = UserDetailsForm(request.POST or None, instance=user)
    if request.method == 'POST':
        if form.is_valid():
            # Save the changes but password
            form.save()

            # Change password
            new_password = form.cleaned_data.get('password')
            if new_password:
                user.set_password(new_pass)
            messages.info(request, 'This is a debug message')
            return HttpResponseRedirect(reverse('account'))

    context = {"form": form}

    return render(request, "iFood/user-account.html", context) 
Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26