2017-05-06 47 views
0

我是Django的新手,嘗試使用CBV爲我的'Project'模型生成表單。當我使用CreateView.as_view(...)urls.py它給出以下錯誤。無法在Django中使用CreateView.as_view

Exception Type:  ImproperlyConfigured 
Exception Value: Using ModelFormMixin (base class of CreateView) without the 'fields' attribute is prohibited. 

我在此列出與此視圖相關的完整代碼。

urls.py

url(r'^create-project/$', CreateView.as_view(model=Project, 
              template_name='en/public/create_project.html', 
              success_url='index'), 
              name='create_project') 

create_project.py

class Form_project_create(forms.Form): 
    class Meta: 
     model = Project 
     fields = '__all__' 

我想不出我們有什麼錯在此代碼。誰能幫忙?

回答

0

您還沒有告訴CreateView使用該表單。

url(r'^create-project/$', CreateView.as_view(form=Form_project_create, ... 

還要注意,表單本身需要從forms.ModelForm,不forms.Form繼承。

0

Form_project_create應該繼承forms.ModelForm,不forms.Form

class Form_project_create(forms.ModelForm): 
    class Meta: 
     model = Project 
     fields = '__all__'