gpt4 book ai didi

python - 尝试连接 ModelForm

转载 作者:行者123 更新时间:2023-12-01 07:47:26 25 4
gpt4 key购买 nike

我收到“值错误”因为“ModelForm 没有指定模型类。”

我尝试检查:models.py forms.py 和views.py,但对我来说一切看起来都很好

views.py:

class CreatePostView(LoginRequiredMixin,CreateView):
login_url='/login/'
redirect_field_name='Myblog/post_detail.html'
form_class = PostForm
model = Post

模型.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)

表单.py:

class PostForm(ModelForm):
class meta:
model = Post
fields = ('author','title','text')

来自 app.urls.pyurl(r'^post/new/$',views.CreatePostView.as_view(),name='post_new'),

最佳答案

<b>M</b>eta 大写,根据 PEP-8类名全部以大写字母开头。在你的表格中,你应该写:

# app/forms.py

class PostForm(ModelForm):

class <b>Meta</b>:
model = Post
fields = ('author','title','text')

既然你把它写成<s>meta</s> ,Django确实没听懂什么model你正在使用。

但是,如果您不编写包含特定项目的表单,您可以 - 例如 @DanielRoseman说,只需将其定义在 CreateView [Django-doc] :

class CreatePostView(LoginRequiredMixin,CreateView):
login_url='/login/'
redirect_field_name='Myblog/post_detail.html'
model = Post
<b>fields = ('author', 'title', 'text')</b>

Django可以通过 modelform_factory [Django-doc]构造表单类.

关于python - 尝试连接 ModelForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56401528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com