我得到这个错误“‘initial’是这个函数的无效关键字参数”,我试图通过createview创建一个对象,这个错误是什么,为什么我得到它?
网址.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

表单.py
   from django import forms
   from .models import Trabalhadores

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

视图.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')

编辑
提交表单后,我得到了这个url
本地主机/添加/?csrfmiddlewaretoken=pvjqynznwjzphuobu1oxnsniekeqmvenixtkr2ufh7iii83qscfgdbrx5hjmzce&Nome=test&Cpf=00000000000&Data=1998-12-14
为什么?

最佳答案

在该视图中将form_类设置为Trabalhadores,这是模型,而不是TrabalhadoresForm,这是表单。

关于python - “initial”是此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41228111/

10-12 17:02