Skip to content Skip to sidebar Skip to footer

Image Uploaded But Not Saved In Media And Database Django

Am creating a settings page where the user should be able to chang there image and biodata. But only the biodata gets updated when I print the request.FILES.GET('profile_picture')

Solution 1:

There is a trailing comma in the line:

#                    trailing comma ↓user.profile_picture=profile_picture,

you need to remove this.

That being said, I would advice to use a ModelForm, this can remove a lot of boilerplate code:

classProfileForm(forms.Form):

    classMeta:
        model = Userfields= ['profile_pic', 'bio']
        labels = {
            'profile_pic': 'Profile picture'
        }
        widgets = {
            'bio': forms.Textarea
        }

Then in the view, we can use:

from django.contrib.auth.decorators import login_required

@login_requireddefsettings(request):
    if request.method == 'POST':
         profile_form = ProfileForm(request.POST, request.FILES, instance=request.user)
         if profile_form.is_valid():
             profile_form.save()
             return redirect('home')
    else:
        profile_form = ProfileForm(instance=request.user)
    context ={
        'profile_form': profile_form
    }
    return render(request, 'user/settings.html', context)

Post a Comment for "Image Uploaded But Not Saved In Media And Database Django"