Django Imagefield not saved

  django, django-forms, python

Although I select images in the form, image changes do not saved. Other inputs are saving. Help me pls.

HTML:

<form method="POST" enctype="multipart/form-data">
                    {% csrf_token %}
                    {{settingform}}
<button type="submit">Save</button>
</form>

models.py:

from django.db import models


class SiteSetting(models.Model):
    logo = models.ImageField(blank=True, verbose_name="Logo")

    @property
    def logo_url(self):
        if self.logo and hasattr(self.logo, 'url'):
            return self.logo.url

views.py:

def SiteSettingsView(request):
    settings = SiteSetting.objects.all()
    settingform = SiteSettingForm(instance=settings[0])
    if request.method == 'POST':
        settingform = SiteSettingForm(request.POST, instance=settings[0])
        if settingform.is_valid():
            settingform.save()
            return redirect('home')
    context = {
        'settings': settings[0],
        'settingform': settingform
    }
    return render(request, 'x.html', context)

forms.py:

from site_setting.models import SiteSetting

class SiteSettingForm(forms.ModelForm):
    class Meta:
        model = SiteSetting
        fields = '__all__'

Source: Python Questions

LEAVE A COMMENT