raise self.model.DoesNotExist( users.models.Profile.DoesNotExist: Profile matching query does not exist.

admin.py


from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import Profile
from .models import CustomUser



class ProfileAdmin(admin.ModelAdmin):
    list_display = ('id', 'user')
    list_display_links = ('id', 'user')
    list_filter = ('user', )
    list_per_page = 20

admin.site.register(Profile, ProfileAdmin)

forms.py


from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Profile
from .models import CustomUser
from django import forms


class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = CustomUser
        fields = ['name', 'email','registration', 'password1', 'password2']

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = CustomUser
        fields = ['name', 'email','registration']

class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

models.py


from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
from PIL import Image


from .managers import CustomUserManager

from django.contrib import admin


class CustomUser(AbstractBaseUser, PermissionsMixin):
    name = models.CharField(max_length=200)
    email = models.EmailField(_('email address'), unique=True)
    registration = models.IntegerField(_('registration'), unique=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email



class Profile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

signals.py



from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import CustomUser


@receiver(post_save, sender=CustomUser)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(email=instance)


@receiver(post_save, sender=CustomUser)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

views.py


from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, Profile


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            email = form.cleaned_data.get('email')
            messages.success(
                request, "Your account has been created! Your ar now able to login.")
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})


@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        # p_form = ProfileUpdateForm(
        #     request.POST, request.FILES, instance=request.user.profile)
        prf = Profile.objects.get(user = request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance = prf)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, "Your account has been updated!")
            return redirect('profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        prf = Profile.objects.get(user = request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance = prf)
    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

full error traceback:


File "C:\Users\zesha\Documents\GitHub\sust-cse-alumni-repo\users\views.py", line 37, in profile
    prf = Profile.objects.get(user = request.user)
  File "C:\Users\zesha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\Loc
alCache\local-packages\Python39\site-packages\django\db\models\manager.py", line 85, in manager_met
hod
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\zesha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\Loc

alCache\local-packages\Python39\site-packages\django\db\models\query.py", line 429, in get
    raise self.model.DoesNotExist(
users.models.Profile.DoesNotExist: Profile matching query does not exist.

This is a copy of raise self.model.DoesNotExist( users.models.Profile.DoesNotExist: Profile matching query does not exist.. Please don’t double-post issues.