<filename>apps/iam/views.py
#from django.core.checks import messages
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.api import success
#from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseRedirect, request
from django.views.generic.edit import (CreateView, DeleteView, FormView,
                                       UpdateView)

from .forms import ProfileUpdateForm
from .models import Profile


class ProfileView(LoginRequiredMixin, UpdateView):
    template_name = "iam/profile.html"
    context_object_name = "profile"
    success_url = "/accounts/profile"
    form_class = ProfileUpdateForm

    def get_object(self):
        """ Return the queryset to get_context_data. """
        querystring = Profile.objects.filter(pk=self.request.user.profile.user_id).first()
        return querystring

    def get_context_data(self, *args, **kwargs):
        """ Return context data to the template. """
        context = super().get_context_data(*args, **kwargs)
        user = self.request.user
        context['form'] = ProfileUpdateForm(
            instance=self.request.user.profile,
            initial={'name': user.name, 'email': user.email}
        )
        return context

    def form_valid(self, form):
        profile = form.save()
        user = profile.user
        user.name = form.cleaned_data['name']
        user.email = form.cleaned_data['email']
        user.save()

        messages.success(self.request, 'Profile was updated successfully')
        return HttpResponseRedirect('/accounts/profile/')
<filename>apps/iam/views.py
#from django.core.checks import messages
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.api import success
#from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseRedirect, request
from django.views.generic.edit import (CreateView, DeleteView, FormView,
                                       UpdateView)

from .forms import ProfileUpdateForm
from .models import Profile


class ProfileView(LoginRequiredMixin, UpdateView):
    template_name = "iam/profile.html"
    context_object_name = "profile"
    success_url = "/accounts/profile"
    form_class = ProfileUpdateForm

    def get_object(self):
        """ Return the queryset to get_context_data. """
        querystring = Profile.objects.filter(pk=self.request.user.profile.user_id).first()
        return querystring

    def get_context_data(self, *args, **kwargs):
        """ Return context data to the template. """
        context = super().get_context_data(*args, **kwargs)
        user = self.request.user
        context['form'] = ProfileUpdateForm(
            instance=self.request.user.profile,
            initial={'name': user.name, 'email': user.email}
        )
        return context

    def form_valid(self, form):
        profile = form.save()
        user = profile.user
        user.name = form.cleaned_data['name']
        user.email = form.cleaned_data['email']
        user.save()

        messages.success(self.request, 'Profile was updated successfully')
        return HttpResponseRedirect('/accounts/profile/')
#from django.core.checks import messages #from django.contrib.messages.views import SuccessMessageMixin Return the queryset to get_context_data. Return context data to the template.