Django1.7: ModelForm 没有指定模型 class
Django1.7: ModelForm has no model class specified
我在 Django 中遇到 "ModelForm has no model class specified." 错误。我花了几个小时来解决这个问题,但看不出问题所在。
非常感谢您!
回溯:
get_response
中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”
111: 响应 = wrapped_callback(请求, *callback_args, **callback_kwargs)
add_category
中的文件“/Users/LandonKoo/code/tango_with_django_project/rango/views.py”
71: 表单 = 类别表单()
init
中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/models.py”
312:引发 ValueError('ModelForm has no model class specified.')
异常类型:/rango/add_category/
处的 ValueError
异常值:ModelForm 没有指定模型 class。
forms.py:
from django import forms
from rango.models import Page, Category
class CategoryForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the category name.")
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
slug = forms.CharField(widget=forms.HiddenInput(), required=False)
print name, views, likes, slug
# An inline class to provide additional information on the form.
class Mata:
# Provide an association between the ModelForm and a model
model = Category
fields = ('name',)
views.py:
def add_category(request):
if request.method=="POST":
form = CategoryForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print form.errors
else:
form = CategoryForm()
return render(request, 'rango/add_category.html', {'form': form})
改变
class Mata:
到
class Meta:
此外,我从未在模型窗体中看到打印语句。以防万一。
我在 Django 中遇到 "ModelForm has no model class specified." 错误。我花了几个小时来解决这个问题,但看不出问题所在。
非常感谢您!
回溯:
get_response
111: 响应 = wrapped_callback(请求, *callback_args, **callback_kwargs) add_category
中的文件“/Users/LandonKoo/code/tango_with_django_project/rango/views.py”71: 表单 = 类别表单() init
中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/models.py”312:引发 ValueError('ModelForm has no model class specified.')
异常类型:/rango/add_category/
处的 ValueError
异常值:ModelForm 没有指定模型 class。
forms.py:
from django import forms
from rango.models import Page, Category
class CategoryForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the category name.")
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
slug = forms.CharField(widget=forms.HiddenInput(), required=False)
print name, views, likes, slug
# An inline class to provide additional information on the form.
class Mata:
# Provide an association between the ModelForm and a model
model = Category
fields = ('name',)
views.py:
def add_category(request):
if request.method=="POST":
form = CategoryForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print form.errors
else:
form = CategoryForm()
return render(request, 'rango/add_category.html', {'form': form})
改变
class Mata:
到
class Meta:
此外,我从未在模型窗体中看到打印语句。以防万一。