1

I have been trying to upload an image in Django using a custom view and I'm failing. The admin panel is letting me upload though, what could be the problem? I have installed Pillow and made sure that the form has the multipart/form-data property. Am I supposed to make a separate model for images?

models.py

class Profile(models.Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE)
    gender = models.TextField(max_length=10)  
    dob = models.DateField(null=True, blank=True)  
    phone_no = models.CharField(unique=True, max_length=9)  
    address = models.CharField(max_length=50)  
    profile_pic = models.ImageField(null=True, blank=True, upload_to="profile_pic/")
    nrc_front = models.ImageField(null=True, blank=True, upload_to="nrc_front/")
    nrc_back = models.ImageField(null=True, blank=True, upload_to="nrc_back/")
    is_verified = models.BooleanField(default=False)
    is_group_admin = models.BooleanField(default=False)
    is_group_member = models.BooleanField(default=False)
    temp_code = models.CharField(max_length=6, null=True, blank=True)

    def __str__(self):
        return str(self.username)

settings.py

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(
    BASE_DIR / 'media'
)

view.py

def updateProfilePic(request):
    profiledata = get_profile_data(request)
    form = UpdateProfilePicForm()
    if request.method == "POST":
        form = UpdateProfilePicForm(request.POST, request.FILES)
        if form.is_valid():
            profile_pic = form.cleaned_data.get("profile_pic")
            Profile.objects.filter(username=request.user).update(profile_pic=profile_pic)
            return redirect('profile')
    else:
        form = UpdateProfilePicForm()
    context = {
        "profiledata":profiledata,
        "form":form
    }
    return render(request, 'base/profile-update-picture.html', context)

template

{% extends 'base/base.html' %}

{% block title %}Update Picture{% endblock %}

{% load crispy_forms_tags %}

{% block content %}
<div class="container">
    <div class="form-container shadow p-3 mb-5 bg-white rounded mx-auto">
        {% crispy form %}
    </div>
</div>
{% endblock %}

browser rendering

<form action="/update-profile-picture/" class="form-group" method="post" enctype="multipart/form-data">

in my url.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

2 Answers 2

2

can you try

Profile.objects.filter(username=request.user)

to:

user_profile = Profile.objects.get(username=request.user)
user_profile.profile_pic = profile_pic
user_profile.save()
Sign up to request clarification or add additional context in comments.

1 Comment

It has worked with this change. Thank you very much.
0

You just change this code from view.py

Profile.objects.filter(username=request.user).update(profile_pic=profile_pic)

To:

Example your table User is this:

class User(models.Model):
    name_user=models.CharField(max_lenght=50)

Change view.py code to:

Profile.objects.filter(username__name_user=request.user).update(profile_pic=profile_pic)

2 Comments

I changed it and it hasn't work
I'm using the default auth_user table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.