Getting user's old image if no image is selected

I have a form that takes in user data like bio, profile picture, gender, etc. and upon submission either creates this new row about the user or updates the existing row. This will only work if the user uploads an image. If no image is uploaded for the profile picture, then the form doesn't submit. How can I make it so that if the user didn't upload a profile picture, then it'll keep the user's previous profile picture and still submit?

Here's my code:

class ProfileSettings(UpdateView):
    model = Profile
    template_name = 'blog/settings.html'
    form_class = ProfileForm
    success_url = reverse_lazy('blog:settings')
def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST, request.FILES)

    if form.is_valid():
        bio = form.cleaned_data['bio']
        gender = form.cleaned_data['gender']
        avatar = form.cleaned_data['avatar']

        Profile.objects.update_or_create(user=self.request.user, defaults={'avatar':avatar, 'bio':bio, 'gender':gender})

    return HttpResponseRedirect(self.success_url)


#django

4 Likes1.45 GEEK