2012-08-01 148 views
2

我尝试将django poll应用程序应用到我自己的应用程序(一个IMC计算器),以改进和构建我在python/django中的第一步的一个小项目。'dict'object has no attribute'create'/'save'

我尝试保存我的第一种形式的数据是accueil.html

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

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

这里是我的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() 

这里是我的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 

最后我* views.py *是在这里: 问题是在该行13 * A = cd.create()*当我尝试保存数据从表单到数据库。

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') 

我想它是在views.py的第一部分,我的功能不能很好地工作,它不会保存表单在数据库中。 高清ACCUEIL(请求):

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/') 

检查,如果表单有效后,我在清理表单中的数据,然后尝试将其保存。 ,我得到这个错误:

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

我想这是从dictionnary,但从来没有在我的代码使用{}为dictionnary名单。 所以我不知道,并陷入学习的过程。

感谢您的帮助,我希望从我的第一个stackoverflow后,我没有犯太多的错误。

回答

3

您应该使用ModelForm,它将自动定义字段以匹配模型上的字段。然后您可以拨打form.save()直接从表单创建实例。

+0

感谢大家的帮助,再次查看关于使用ModelForm的代码我使用的是Forms.py中的IndividuDataForm,并使用models.py中的IndividuForm进行更改,它的工作原理是!!! – Tony 2012-08-01 21:10:05

2

丹尼尔的回答是更正确的

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

我会离开我原来的答复,虽然,只是为了文档链接。

form.cleaned_data是一种字典类型,并且没有create()方法。我假设你正试图为数据创建一个模型到数据库中?

查看Can a dictionary be passed to django models on create?以从该字典创建模型。一个示例可能是:

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

以下是处理表单数据的文档。

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

祝你好运!

-2

如果您只是想将提交的数据保存为django ModelForm,请使用form.save()。

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

这将已经在使用清洁/验证的数据。

如果您需要做某事,您可以使用form.cleaned_data。而不是用模型实例保存它。

+0

-1:你的答案的哪一部分未被丹尼尔或瑞恩所涵盖? – 2012-08-01 20:15:50

+0

无。只是没有看到他们。 – alex 2012-08-01 20:20:06

相关问题