Django : Even if my form is valid , I got some fields empty when printing cleaned_data

112 Views Asked by At

My model:

class sarl(models.Model):
    denomination_social = models.CharField(max_length=200,blank=True)
    numero_certificat = models.CharField(max_length=15,blank=True)
    myfile = models.FileField(blank=True)
    adresse = models.CharField(max_length=250,blank=True)
    activite = models.CharField(max_length=100,blank=True)
    objet = models.CharField(max_length=100,blank=True)
    autreactiv = models.CharField(max_length=1000,blank=True)
    ville = models.CharField(max_length=20,blank=True, null=True)
    exercice_dip = models.BooleanField(default=False,blank=True)
    capital_social=models.FloatField(max_length=15,blank=True)
    gerant_representant = models.ForeignKey(PerP,on_delete=models.CASCADE,blank=True, null=True)
    gerant_sep = models.ForeignKey(gerant_separe,blank=True, null=True,on_delete=models.CASCADE)
    Typesarl = models.BooleanField(default=False,blank=True)
    nbr_associe = models.CharField(max_length=6,default="--")
    nbr_gerants = models.CharField(max_length=6,default="--")

My modelform:

class sarlform(forms.ModelForm):
    nbr_associe = forms.ChoiceField(choices=NBR_ASSOCIE, widget=forms.Select(attrs={'onchange':'NbrAssocie()'}),required = False)
    autreactiv = forms.CharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 7}))

    class Meta:
        model = sarl
        fields = (
            'denomination_social', 'numero_certificat', 'adresse',
            'activite', 'autreactiv', 'capital_social', 'nbr_associe',
        )

when I want to post cleaned data of my form on my views.py , my form is valid, and I get all my fields data but I dont get the adresse field.

def saveToBase(request):
    form = sarlform(request.POST)
    if form.is_valid(): 
        print(request.POST.get('CuisineList'))
        print("sarl form is valid !")
        print(form.cleaned_data)
        print(request.POST['adresse'])
        form.save()  

This is what I get from printing cleaned data :

{'denomination_social': 'fkrbvirbv', 'numero_certificat': 'bybyb',
 'adresse': '', 'ville': None, 'activite': 'gggg', 'autreactiv': 'gregetg',
 'capital_social': 140000.0, 'nbr_associe': '2'}

I've been stucking here for hours, and really dont get what is the reason of this problem. Any help Please would be awesome.

1

There are 1 best solutions below

0
On

I discovred where my problem was : in my HTML I called Adresse and ville two times, so when I posted cleaned data it only takes default value

My HTML form was :

<div id="domiciliation" style="display: none">
                    <p>Adresse : {{form.adresse}}</p>
                    <p>Ville : {{form.ville}}</p>
                </div>
<div id="siege_social" style="display: none">
                    <p>Adresse : {{form.adresse}}</p>
                    <p>Ville : {{form.ville}}</p>
                </div>

when I created different fields for the second div it work just fine as bellow :

<div id="domiciliation" style="display: none">
                        <p>Adresse : {{form.adresse}}</p>
                        <p>Ville : {{form.ville}}</p>
                    </div>
 <div id="siege_social" style="display: none">
                    <p>Adresse : {{form.aad}}</p>
                    <p>Ville : {{form.vi}}</p>
                </div>

I would thank everyone for their replays.