I am working on a project that require an profile pic. I created a Model in Django UserProfile
class UserProfile(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, primary_key=True)
image = models.ImageField()
adress = models.CharField(default='', max_length=150, blank=True)
cnic = models.IntegerField(null=True)
contact = models.IntegerField(null=True)
city = models.CharField(max_length=50, null=True)
about = models.CharField(max_length=50, null=True)
location = models.CharField(max_length=150, null=True)
subscriptions = models.IntegerField(null=True)
rating = models.IntegerField(null=True)
I am currently fetching data from HTML
<form action="/users/profile/" method="POST" style="display: contents;">
{% csrf_token %}
<div class="col-md-3 border-right">
<div class="d-flex flex-column align-items-center text-center p-3 py-5">
{% if profile.image %}
<img class="rounded-circle mt-5" src="/media/{{profile.image}}"
style="width: 200px;max-height: 300px;" id='image'>
{% else %}
<img class="rounded-circle mt-5"
src="https://image.shutterstock.com/image-vector/house-not-available-icon-flat-260nw-1030785001.jpg"
style="width: 200px;max-height: 300px;" id='image'>
{% endif %}
<label for="upload-photo" class="uploadImgLabel btn btn-outline-danger w-75">Browse</label>
<input type="file" name="photo" id="upload-photo" required />
<span class="font-weight-bold">{{user}}</span><span
class="text-black-50">{{user.email}}</span><span>
</span>
</div>
All the other field is working correctly and I can visualize data by printing in views.py
if request.method == 'POST':
photo = request.POST.get('photo')
fname = request.POST.get('firstname')
lastname = request.POST.get('lastname')
contact = request.POST.get('contact')
address = request.POST.get('address')
email = request.POST.get('email')
country = request.POST.get('country')
cnic = request.POST.get('cnic')
city = request.POST.get('city')
user = User.objects.get(username=request.user)
user.first_name = fname
user.last_name = lastname
user.email = email
user.save()
obj = models.UserProfile.objects.get(user=request.user)
obj.adress = address
# obj.image = photo, <---- Here is the problem
obj.contact = contact
obj.city = city
obj.cnic = cnic
obj.subscriptions = 100
obj.rating = 4
obj.save()
I am unable to save data in obj.save() its shows an error when I try to put an image like obj.image=photo
Error AttributeError at /users/profile/ 'tuple' object has no attribute '_committed
I am swore it is because of the image if i visualize image (print(photo)) its shows file name 'Image.jpg' '