Images getting duplicated on django_forms

34 views Asked by At


I have a web template where i am uploading two photos on different fields, but when i save the form one of them gets replaced with the other.
Ex. i upload an image on field-1 but when i save the form the image gets saved on field-1 and field-2, it happens the both ways, when uploading something on field-2 also gets replaced on field-1

FormCode

class CustomLPForm(forms.ModelForm):
    address = forms.CharField()
    we_ara_picture = forms.ImageField( required=True)
    landing_image = forms.ImageField( required=True)
    landing_image_mobile = forms.ImageField( required=False)

    def __init__(self, *a, **kw):
        super(CustomLPForm, self).__init__(*a, **kw)

    class Meta:
        model = CustomLP
    
        exclude = ("date_created", "date_modified", "date_removed", "agency")
        error_messages = {
            'about': {
            'max_length': _("La descripcion es muy larga."),
            },
        }

Model

    class CustomLP(Model):

    agency = models.OneToOneField(
        Agency, related_name="landing_customization", verbose_name=_("Agency"), unique=False)
    we_are = models.TextField(verbose_name=_("We are"), null=True, blank=True)
    we_ara_picture = models.ImageField(
        upload_to=logo_picture_upload_to,
        verbose_name=_("we ara picture"), null=True, blank=True)
    landing_image = models.ImageField(
        upload_to=logo_picture_upload_to,
        verbose_name=_("landing_image"), null=True, blank=True)
    landing_image_mobile = models.ImageField(
        upload_to=logo_picture_upload_to,
        verbose_name=_("landing_image_mobile"), null=True, blank=True)

View

    def customize_landing(request):
        agency = request.user.profile.user_agency
        if request.POST:
            landing = agency.landing_customization
        

            landing_form = CustomLPForm(
            request.POST, request.FILES, instance=landing, prefix="customlp")
            if not landing_form.is_valid():
                msg = u"Ocurrió un error al intentar configurar la web."
                messages.error(request, msg)
            else:     
            
                landing = landing_form.save(commit=False)
                landing.agency = agency
            
                storaged_agency = landing.agency

                storaged_agency.latitude = request.POST['latitude']
                storaged_agency.longitude = request.POST['longitude']
                storaged_agency.microsite_domain = request.POST['customlp-microsite']
                storaged_agency.address = request.POST['customlp-address']

                storaged_agency.save()
                landing.cards = json.dumps(cards_dict)
                landing.save()
                msg = u"El sitio web de la agencia ha sido publicado."
                messages.success(request, msg)
0

There are 0 answers