mercredi 11 novembre 2015

how to test forms in django?

My models looks like this:

class Car(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    name = models.CharField(max_length=200)

class Owner(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    car = models.ForeignKey(Car)

and a form that looks like this

class CarForm (forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(CarForm, self).__init__(*args, **kwargs)
        self.fields['car_name']=forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}),label='', required=False)
        self.fields['person_name']=forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}),label='', required=False)

So when the user goes to my index file he find two forms, one for his name and one for the car's name which when submitted will be created in the database then.

So now I am in the shell and want to test that and I'm not sure what the correct sytnax is, I tried this:

response = client.post('/', {'car_name':'something','person_name':'something'})

but I always get a

IndexError: list index out of range

What does that mean? Or what's the correct syntax to run the tests?

I also tried

response = client.post('/', {'id_car_name':'something','id_first_name':'something'})

Since these are the ids that django creates in the homepage, but didn't work

Aucun commentaire:

Enregistrer un commentaire