Django无法创建基于模型的表单:" str对象不可调用"

时间:2014-03-16 16:37:48

标签: django django-forms

我正在尝试基于模型创建表单

urlpatterns = patterns('inventory.views',
    url(r'^template/create/$', CreateView.as_view(form_class='ItemTemplateForm', template_name='generic_form.html'), name='template_create'),

class ItemTemplateForm(forms.ModelForm):
    class Meta:
        model = ItemTemplate
        exclude = ('photos', 'supplies', 'suppliers')

这会引发错误:

TypeError at /inventory/template/create/
'str' object is not callable
Request Method: GET
Request URL:    http://127.0.0.1:8000/inventory/template/create/
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
'str' object is not callable
Exception Location: c:\Program Files (x86)\Python\lib\site-packages\django\views\generic\edit.py in get_form, line 44

我在抛出异常时设置了跟踪,但这也没有帮助。这个错误的原因是什么?

1 个答案:

答案 0 :(得分:2)

我认为在您的网址中您应该传递表单类ItemTemplateForm,而不是字符串名称:

from app.form import ItemTemplateForm

urlpatterns = patterns('inventory.views',
    url(r'^template/create/$', CreateView.as_view(form_class=ItemTemplateForm, template_name='generic_form.html'), name='template_create'),