'initial'是此函数的无效关键字参数

时间:2016-12-19 17:38:56

标签: python django django-forms django-views django-class-based-views

我收到此错误“'initial'是此函数的无效关键字参数”,我正在尝试通过createview创建一个对象,这是什么错误以及为什么我会得到它?

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^add/$', views.CriarTrabalhador.as_view(), name='add')]

models.py

from django.db import models

    # Create your models here.

    class Trabalhadores(models.Model):
        Nome = models.CharField('Name',max_length=100)
        Cpf = models.CharField(primary_key=True,max_length=11)
        Data = models.DateField('Data de Nascimento')

        def __str__(self):
            return self.Nome

forms.py

   from django import forms
   from .models import Trabalhadores

    class TrabalhadoresForm(forms.ModelForm):
        class Meta:
           model = Trabalhadores
                fields = ["Nome","Cpf","Data"]

views.py

from django.shortcuts import render
from django.views.generic.list import ListView
from django.core.urlresolvers import reverse
from .models import Trabalhadores
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView,CreateView
from . import forms

class CriarTrabalhador(CreateView):
    model = Trabalhadores
    form_class = forms.Trabalhadores
    template_name = 'mytemp/adicionartrabalhador.html'

    def get_success_url(self):
        return reverse('trabalhadores')

编辑

提交表单后,我收到了这个网址

本地主机/添加/ csrfmiddlewaretoken = PVjqYnZNwJZpHuObu1OXNSnIekeqMVVeNIIxTkr2UFH7ii83QScfGdBrX5HjmZCe&安培;诺姆=试验&安培; CPF = 00000000000&安培;数据= 1998-12-14

为什么?

2 个答案:

答案 0 :(得分:6)

您已将该视图中的form_class设置为Trabalhadores,而不是TrabalhadoresForm,即形式。

答案 1 :(得分:0)

你必须把你的form_class等于你的表格

与form_class = TrabalhadoresForm一样,因为您使用from class创建视图,数据将通过您的模型类传递 对于所有类型的Django泛型类视图都是一样的