Ali Rahmani
Ali Rahmani

Reputation: 51

image not updating in django from form post request

I am trying to edit an object through the form

All fields are edited but the image field does not change

What is the problem?

model created with signals when user create account

template

 <div>
      <form action="{% url 'user:profile' %}" method="post" enctype="multipart/form-data">
          {% csrf_token %}
          {{form}}
          <input type="submit" value="submit">
      </form>
  </div>

model

class Profile(models.Model):
    CHOICES = (
        ('RE', 'مشاور املاک'),
        ('S', 'فروشنده'),
    )

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    real_estate = models.OneToOneField(Business , related_name='business' ,on_delete=models.CASCADE, blank=True , null=True)
    image = models.ImageField(blank=True)
    type_of_user = models.CharField(max_length=300, choices=CHOICES)
    phone_number = PhoneNumberField(unique = True, null = False, blank = False)

form

class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        fields = ['type_of_user' , 'phone_number' , 'image']

views

def profile(request):
    profile = Profile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ProfileForm(request.POST , instance=profile)
        profile = form.save(commit=False)
        profile.user = request.user
        profile.save()
        redirect('busi:index')
    else:
        form = ProfileForm()
    context = {
        'profile':profile,
        'form':form
    }

    return render(request , 'user/profile.html' , context)

Upvotes: 0

Views: 185

Answers (1)

Shreyash mishra
Shreyash mishra

Reputation: 790

image is a type of file so you have to request files while requesting forms to be saved

def profile(request):
    profile = Profile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ProfileForm(request.POST , request.FILES, instance=profile)
        profile = form.save(commit=False)
        profile.user = request.user
        profile.save()
        redirect('busi:index')
    else:
        form = ProfileForm()
    context = {
        'profile':profile,
        'form':form
    }

    return render(request , 'user/profile.html' , context)

also seems that you did not write a path for image to be stored in your model

class Profile(models.Model):
    CHOICES = (
        ('RE', 'مشاور املاک'),
        ('S', 'فروشنده'),
    )

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    real_estate = models.OneToOneField(Business , related_name='business' ,on_delete=models.CASCADE, blank=True , null=True)
    image = models.ImageField(blank=True, upload_to='profile_pics')
    type_of_user = models.CharField(max_length=300, choices=CHOICES)
    phone_number = PhoneNumberField(unique = True, null = False, blank = False)

create folder name it as you want in media folder and code it there where image can be stored so django can look up for your image in that folder

Upvotes: 1

Related Questions