我正在接收“值错误” 因为“ ModelForm没有指定模型类。”
我试图检查:models.pyforms.py和views.py,但对我来说一切都很好
views.py:
class CreatePostView(LoginRequiredMixin,CreateView):
login_url='/login/'
redirect_field_name='Myblog/post_detail.html'
form_class = PostForm
model = Post
models.py:
class Post(models.Model):
author = models.ForeignKey('auth.User',on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True,null=True)
forms.py:
class PostForm(ModelForm):
class meta:
model = Post
fields = ('author','title','text')
来自app.urls.py url(r'^ post / new / $',views.CreatePostView.as_view(),name ='post_new'),
答案 0 :(得分:1)
Meta
带有大写,根据PEP-8,类的名称均以大写字母开头。在您的表格中,您应该输入:
# app/forms.py
class PostForm(ModelForm):
class Meta:
model = Post
fields = ('author','title','text')
由于您将其编写为
,因此Django确实不理解您使用的metamodel
。
但是,如果您不编写包含特定项目的表格,则可以-像@DanielRoseman所说的那样,只需在CreateView
[Django-doc]处进行定义:
class CreatePostView(LoginRequiredMixin,CreateView):
login_url='/login/'
redirect_field_name='Myblog/post_detail.html'
model = Post
fields = ('author', 'title', 'text')
Django可以通过modelform_factory
[Django-doc]构造一个表单类。