3

I try to apply the django poll application to my own application (an IMC calculator) to improve and build a little project for my first step in python/django.

I try to save the data of my first form that is on accueil.html:

<form action="" method="post">
  <table>
    {{ form.as_table }}
  </table>

 <input type ="submit" value="Submit">
</form>

Here is my forms.py:

from django import forms

class IndividuDataForm(forms.Form):
    nom = forms.CharField(max_length=100)
    prenom = forms.CharField(max_length= 100)
    anniversaire = forms.DateField()
    taille = forms.IntegerField()
    poids = forms.IntegerField()

Here is my models.py:

from django.db import models

from django.forms import ModelForm

class Individu(models.Model):

    nom = models.CharField(max_length=100)
    prenom = models.CharField(max_length= 100)
    anniversaire = models.DateField()
    taille = models.IntegerField()
    poids = models.IntegerField()

    def __unicode__(self):

       return self.nom

class ImcResultat(models.Model):

    individu = models.ManyToManyField(Individu)

    imc = models.IntegerField()

    date_imc = models.DateField()

class IndividuForm(ModelForm):

    class Meta:

        model = Individu


class ImcResultatForm(ModelForm):

    class Meta:

        model = ImcResultat

And finally my *views.py* is here: the problem is at the line 13 * a = cd.create() * when I try to save the data from the form to the database.

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from models import Individu, ImcResultat
from forms import IndividuDataForm

def accueil(request):

    if request.method == 'POST':

        form = IndividuDataForm(request.POST)

        if form.is_valid():

            cd = form.cleaned_data

            a = cd.create() **#this is the problem**

            return HttpResponseRedirect('/accueil/page2/')

    else:

        form = IndividuDataForm()

        return render_to_response('accueil.html', {'form':form})





def page2(request):



    return render_to_response('page2.html')

i guess it is on the first part of the views.py that my function do not work well, it does not save the form in the database. def accueil(request):

if request.method == 'POST':

    form = IndividuDataForm(request.POST)

    if form.is_valid():

        cd = form.cleaned_data

        a = cd.create() **#this is the problem**

        return HttpResponseRedirect('/accueil/page2/')

after checking if the form is valid, I clean the data in the form then try to save it. and I got this error:

Exception Type: AttributeError 
Exception Value: 'dict' object has no attribute 'create'

I guess it is from a dictionnary but never in my code i use { } for dictionnary list. So I have no idea and stuck in the process of learning.

Thanks for your help and I hope from my first stackoverflow post I didn't made too many mistakes.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tony
  • 394
  • 4
  • 14

3 Answers3

3

Daniel's answer is more correct

if form.is_valid():
    form.save()

I'll leave my original answer though, just for the documentation links.

The form.cleaned_data is a dictionary type, and doesn't have the create() method. I assume you are trying to create a model for that data into your database?

Take a look at Can a dictionary be passed to django models on create? to create a model from that dictionary. An example may be:

cd = form.cleaned_data
themodel = Individu(**cd)
themodel.save()

Here is the documentation for processing the form data as well.

https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form

Good luck!

Community
  • 1
  • 1
ryanday
  • 2,506
  • 18
  • 25
3

You should use a ModelForm, which will define the fields automatically to match those on the model. Then you can call form.save() to create the instance directly from the form.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank everyone for your help, by looking again concerning the code use with ModelForm I was using IndividuDataForm from the forms.py and I change it by using IndividuForm from models.py and it works !!! – Tony Aug 01 '12 at 21:10
-2

If you just want the submitted data to be saved as a django ModelForm, use form.save().

see: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#s-the-save-method

This will already use cleaned/validated data.

You can use form.cleaned_data if you need to do sth. else than saving it with a model instance.

alex
  • 1,229
  • 7
  • 6