新表单模型不工作 - 异常值:没有这样 table
New Form Model Not Working - Exception Value: no such table
我为我的博客网站创建了一个评论表单,但我无法让该表单正常工作。我可以从管理页面 post 为每个博客 post 发表评论,但是当我尝试将表单提交到 post 时,我收到的评论 table 不存在错误。
我研究了很多问题,人们遇到了同样的错误,但似乎没有一个与我遇到的问题直接相关。我已经 运行 ./manage.py makemigrates && ./manage.py migrate 并且显示我没有待处理的迁移。任何帮助将不胜感激。
这是我的相关代码:
blog/models.py
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
#tag manager
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#get feature image
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
def serve(self, request):
from .forms import CommentForm
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.page_id = self.id
comment.save()
return redirect(self.url)
else:
form = CommentForm()
return render(request, self.template, {
'page': self,
'form': form,
})
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
], heading="Blog information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
InlinePanel('comments', label='Comments'),
]
class BlogPageComment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
comment_author = models.CharField(max_length=50)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
class Meta:
abstract = True
class BlogPageComments(Orderable, BlogPageComment):
page = ParentalKey('BlogPage', related_name='comments')
blog/forms.py
from django import forms
from .models import BlogPageComment
class CommentForm(forms.ModelForm):
class Meta:
model = BlogPageComment
fields = ('comment_author', 'text',)
错误:
环境:
Request Method: POST
Request URL: http://127.0.0.1:8000/blog/my-blog-post/
Django Version: 1.10.6
Python Version: 2.7.12
Installed Applications:
[u'home',
u'search',
u'wagtail.wagtailforms',
u'wagtail.wagtailredirects',
u'wagtail.wagtailembeds',
u'wagtail.wagtailsites',
u'wagtail.wagtailusers',
u'wagtail.wagtailsnippets',
u'wagtail.wagtaildocs',
u'wagtail.wagtailimages',
u'wagtail.wagtailsearch',
u'wagtail.wagtailadmin',
u'wagtail.wagtailcore',
u'wagtail.contrib.modeladmin',
u'modelcluster',
u'taggit',
u'django.contrib.admin',
u'django.contrib.auth',
u'django.contrib.contenttypes',
u'django.contrib.sessions',
u'django.contrib.messages',
u'django.contrib.staticfiles',
u'blog']
Installed Middleware:
[u'django.contrib.sessions.middleware.SessionMiddleware',
u'django.middleware.common.CommonMiddleware',
u'django.middleware.csrf.CsrfViewMiddleware',
u'django.contrib.auth.middleware.AuthenticationMiddleware',
u'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
u'django.contrib.messages.middleware.MessageMiddleware',
u'django.middleware.clickjacking.XFrameOptionsMiddleware',
u'django.middleware.security.SecurityMiddleware',
u'wagtail.wagtailcore.middleware.SiteMiddleware',
u'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
42. response = get_response(request)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/wagtail/wagtailcore/views.py" in serve
27. return page.serve(request, *args, **kwargs)
File "/home/kenneth/development/web/sites/mysite/dynamicsalesops/blog/models.py" in serve
69. comment.save()
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in save
796. force_update=force_update, update_fields=update_fields)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
824. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
889. forced_update)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_update
939. return filtered._update(values) > 0
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/query.py" in _update
654. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1148. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
835. cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
337. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /blog/my-blog-post/
Exception Value: no such table: blog_blogpagecomment
BlogPageComment
的 table 不存在,因为您已将其定义为抽象模型 - 相应的具体模型( 有table) 是 BlogPageComments
。我认为像这样拆分它没有任何价值 - 我建议按照下面的代码片段将其定义为单个模型定义。
我还建议 不要 在 BlogPage
模型上给它一个 ParentalKey 和 InlinePanel,因为评论实际上并不是页面内容本身的一部分——它们不不会与页面内容的其余部分一起进行版本控制或审核。
class BlogPageComment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
page = ForeignKey('BlogPage', related_name='comments')
comment_author = models.CharField(max_length=50)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
我为我的博客网站创建了一个评论表单,但我无法让该表单正常工作。我可以从管理页面 post 为每个博客 post 发表评论,但是当我尝试将表单提交到 post 时,我收到的评论 table 不存在错误。
我研究了很多问题,人们遇到了同样的错误,但似乎没有一个与我遇到的问题直接相关。我已经 运行 ./manage.py makemigrates && ./manage.py migrate 并且显示我没有待处理的迁移。任何帮助将不胜感激。
这是我的相关代码:
blog/models.py
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
#tag manager
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
#get feature image
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
def serve(self, request):
from .forms import CommentForm
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.page_id = self.id
comment.save()
return redirect(self.url)
else:
form = CommentForm()
return render(request, self.template, {
'page': self,
'form': form,
})
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
], heading="Blog information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
InlinePanel('comments', label='Comments'),
]
class BlogPageComment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
comment_author = models.CharField(max_length=50)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
class Meta:
abstract = True
class BlogPageComments(Orderable, BlogPageComment):
page = ParentalKey('BlogPage', related_name='comments')
blog/forms.py
from django import forms
from .models import BlogPageComment
class CommentForm(forms.ModelForm):
class Meta:
model = BlogPageComment
fields = ('comment_author', 'text',)
错误:
环境:
Request Method: POST
Request URL: http://127.0.0.1:8000/blog/my-blog-post/
Django Version: 1.10.6
Python Version: 2.7.12
Installed Applications:
[u'home',
u'search',
u'wagtail.wagtailforms',
u'wagtail.wagtailredirects',
u'wagtail.wagtailembeds',
u'wagtail.wagtailsites',
u'wagtail.wagtailusers',
u'wagtail.wagtailsnippets',
u'wagtail.wagtaildocs',
u'wagtail.wagtailimages',
u'wagtail.wagtailsearch',
u'wagtail.wagtailadmin',
u'wagtail.wagtailcore',
u'wagtail.contrib.modeladmin',
u'modelcluster',
u'taggit',
u'django.contrib.admin',
u'django.contrib.auth',
u'django.contrib.contenttypes',
u'django.contrib.sessions',
u'django.contrib.messages',
u'django.contrib.staticfiles',
u'blog']
Installed Middleware:
[u'django.contrib.sessions.middleware.SessionMiddleware',
u'django.middleware.common.CommonMiddleware',
u'django.middleware.csrf.CsrfViewMiddleware',
u'django.contrib.auth.middleware.AuthenticationMiddleware',
u'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
u'django.contrib.messages.middleware.MessageMiddleware',
u'django.middleware.clickjacking.XFrameOptionsMiddleware',
u'django.middleware.security.SecurityMiddleware',
u'wagtail.wagtailcore.middleware.SiteMiddleware',
u'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
42. response = get_response(request)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/wagtail/wagtailcore/views.py" in serve
27. return page.serve(request, *args, **kwargs)
File "/home/kenneth/development/web/sites/mysite/dynamicsalesops/blog/models.py" in serve
69. comment.save()
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in save
796. force_update=force_update, update_fields=update_fields)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
824. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
889. forced_update)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_update
939. return filtered._update(values) > 0
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/query.py" in _update
654. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1148. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
835. cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/kenneth/development/web/sites/mysite/wagtailenv/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
337. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /blog/my-blog-post/
Exception Value: no such table: blog_blogpagecomment
BlogPageComment
的 table 不存在,因为您已将其定义为抽象模型 - 相应的具体模型( 有table) 是 BlogPageComments
。我认为像这样拆分它没有任何价值 - 我建议按照下面的代码片段将其定义为单个模型定义。
我还建议 不要 在 BlogPage
模型上给它一个 ParentalKey 和 InlinePanel,因为评论实际上并不是页面内容本身的一部分——它们不不会与页面内容的其余部分一起进行版本控制或审核。
class BlogPageComment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
page = ForeignKey('BlogPage', related_name='comments')
comment_author = models.CharField(max_length=50)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)