0

I am trying to save the image without using Django forms in the media folder. The image should save in the media\seller_profile folder and its path has to be entered in a database like seller_profile\abc.png but somehow I am getting only abc.png missing seller_profile.

Models.py

class SellerProfile(models.Model):
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255, default=None, null=True)
    seller_profile_picture = models.ImageField(upload_to='seller_profile', default="seller_profile/empty_profile.png")

    def __str__(self):
        return self.email

Views.py

def profile(request):
    
    if request.method == "POST":
        
        profile_data = SellerProfile.objects.filter(email = str(request.user)).update(
                                                    first_name = request.POST.get("First name"),
                                                    seller_profile_picture = request.FILES["seller profile picture"],
                                                    )
        print("object File --->", request.FILES["seller profile picture"])
        return redirect("/seller/profile")
    else:
        user_data = SellerProfile.objects.filter(email=str(request.user))
        if len(user_data) == 0:
            SellerProfile.objects.create(email = str(request.user))
            data_entered = {"First name": "Value not Provided"}
        else:
            data_entered = dict()
            data_entered["First name"] = user_data[0].first_name

        return render(request, "seller/profile.html", {"data_entered":data_entered})

seller/profile.html

{% extends 'general/layout.html'%}

{% block body %}

<div class="container">
    <div class="jumbotron">
        <form method="post" enctype=multipart/form-data>
            {%  csrf_token %}
            {% for attribute, placeholder in data_entered.items %}
                <div class="form-group">
                    <dt><label for="{{attribute}}">{{attribute}}</label>
                    <dd><input class="form-control" id="{{attribute}}" name="{{attribute}}"
                               type="text" placeholder="{{placeholder}}" required>
                    </dd>

                </div>
            {% endfor %}

            <dt><label for="seller profile picture">Profile Picture</label>
                    <dd><input class="form-control" id="seller profile picture" name="seller profile picture"
                               type="file" required>
                    </dd>
            </dt>


            <div class="form-group text-center">
                <p>
                    <input class="btn btn-primary " type="submit" value="Update">
                </p>
            </div>

        </form>
    </div>
</div>

{% endblock%}

Updating Models.py by referring link. According to this solution update do not call save.

def profile(request):
     if request.method == "POST":
            print("get ", SellerProfile.objects.get(email = str(request.user)))
            print("type get ", type(SellerProfile.objects.get(email = str(request.user))))
 
            profile_data = SellerProfile.objects.get(email = str(request.user))
            profile_data.email = str(request.user),
            profile_data.first_name = request.POST.get(seller_char_attribute[0]),
            profile_data.seller_profile_picture = request.FILES["seller profile picture"],
            profile_data.save()

but this gives me error as follow

 Virtialenv\lib\site-packages\django\db\models\fields\files.py",line 286, in pre_save
        if file and not file._committed:
    AttributeError: 'tuple' object has no attribute '_committed'
raviraj
  • 335
  • 4
  • 19

2 Answers2

1

You've left trailing commas at the end of some of the lines where you're assigning values to the profile_data object. Those commas mean that tuples will be assigned rather than the values you intend:

profile_data.email = str(request.user),
profile_data.first_name = request.POST.get(seller_char_attribute[0]),
profile_data.seller_profile_picture = request.FILES["seller profile picture"],

Removing the trailing commas from the above lines should resolve.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61
0

In your profile function def profile(request):

where you are dealing with image

profile_data.seller_profile_picture = request.FILES["seller profile picture"],

Instead of this you have to use request.FILES.get()

An instead of passing the image name in list type ["seller profile picture"]you have to give only image name

So at last your line will look like this

profile_data.seller_profile_picture = request.FILES.get("seller profile picture")
Rajat Lad
  • 1
  • 1