Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Edit Profile View not saving new email value

I am (attempting to) implement the ability for a user to edit and update their email address on their profile page. I am getting no errors when doing this end to end but the new email is not being saved to the DB.

Everything seems to be working, even the redirect to the profile page in the edit_profile function, but the save() doesn’t seem to be working, the users email doesn’t update and when I am redirected back to the profile page, the email is still the current value.

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Model:

class CustomUser(AbstractUser):
email = models.EmailField(_('email address'), unique=True)
is_pro = models.BooleanField(default=False)
is_golfer = models.BooleanField(default=False)

def __str__(self):
    return self.email

Form

class EditProfileForm(forms.Form):
email = forms.EmailField(
    label='', widget=forms.TextInput(attrs={'class': 'form-field'}))

View

@login_required
def edit_profile(request):
if request.method == "POST":
    form = EditProfileForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data["email"]
        
        user = CustomUser.objects.get(id=request.user.id)
        user.save()
    return redirect("typeA", username=user.username)
else:
    form = EditProfileForm()
return render(request, "registration/edit_profile.html", {'form': form})

URLS

urlpatterns = [
path('type_a_signup/', ASignUpView.as_view(), name='a_signup'),
path('type_b_signup/', BSignUpView.as_view(), name='b_signup'),
path('login/', LoginView.as_view(), name='login'),
path('password_reset', PasswordResetView.as_view(), name='password_reset'),
path('typea/<username>/', typeA, name='typeA'),
path('typeb/<username>/', typeB, name='typeB'),
path('login_success/', login_success, name='login_success'),
path('edit_profile/', edit_profile, name='edit_profile'),
]

Template

<div class="container">
<div class="form-container">
    <h2>Edit profile</h2>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <div>
            {{ form.email.label_tag }}
            <input type="text" class="form-control {% if form.email.errors %}is-invalid{% endif %}" id="id_email"
                name="email" value='{{ form.email.value|default:user.email }}'>
            {% if form.email.errors %}
            <div>{{ form.email.errors }}</div>
            {% endif %}
        </div>
        <button type="submit">Submit</button>
    </form>
    <br>
</div>

>Solution :

You never set the email field of the object. You should set this with:

@login_required
def edit_profile(request):
if request.method == "POST":
    form = EditProfileForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data["email"]
        user = request.user
        user.email = email  # 🖘 set the email field
        user.save()
        return redirect("typeA", username=user.username)
else:
    form = EditProfileForm()
return render(request, "registration/edit_profile.html", {'form': form})

You should only redirect in case the form is successful. If it is not, Django will rerender the form with the errors.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading