- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在我的应用程序中添加了一个 Comment 模型,但是当提交表单时,弹出这个错误:
NOT NULL 约束失败:blog_comment.post_id
这是模型.py:
class Post(models.Model):
title = models.CharField(max_length=100, null=True, blank=True)
caption = models.CharField(max_length=100, null=True, blank=True)
image = models.ImageField(upload_to='post_pics/', null=True, blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
text = models.TextField(null=True)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.text
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
这是在 views.py 中创建评论的部分:
class AddCommentView(LoginRequiredMixin, CreateView):
model = Comment
fields = ['text']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
我的表格.py:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['text']
html 格式:
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">New Comment</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Publish</button>
</div>
</form>
</div>
最佳答案
您必须为 post
设置值,因为它是一个外键。在 form_valid
中,您应该定义 post = Post.objects.get(id=current_post_id)
并且您可以从 url 获取 current_post_id
或将其接收为一个参数。然后赋值:form.instance.post = post
关于django - "IntegrityError. NOT NULL constraint failed: blog_comment.post_id "代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872523/
我有以下 MySQL 命令: SELECT CASE WHEN pm.meta_key = 'webinar_date' AND pm.meta_value = '20190521' THEN pm.
我的 mysql 查询是: $query=('SELECT count(post_id) FROM comments where post_id='.$row['post_id']); $us
我想为新帖子创建 ID,例如 00001、00002,但是,同时我需要检索post_id来命名图片URL xxx/00001_1.jpg。那么我应该用 mysql(auto_increment) 还是
这是设置。 id ) . '">' ;?> Setting ';?> id ) . '">' ;?> Content ';?> jQuery(".rh_content[data-p
我创建了一个带有提货输入的表单,因此当用户输入德里机场作为提货位置并输入阿格拉作为投递位置时,它应该返回 post_ID = 2 但我已经使用 WHERE LIKE 进行了查询这导致所有 ID 在 p
我使用 mysqlnd 5.0.12-dev 数据库作为后端,我想返回每个 post_id 的 wp_productprofitability 中的最后一行。 我有以下设置: wp_post: +--
我正在使用 FBDialogs 共享一个链接,作为跟踪的一部分,我想将该共享帖子的 post_id 返回给服务器。 如何在成功时检索 post_id [FBDialogs presentShareDi
我正在尝试进行简单的 mysql 选择查询,我有 3 个表 post: post_id... tags: tag_id, tag_name post_tag: id_post, id_tag 然后我写
你好,我有一个 mysql 查询,它会根据状态的最大计数返回 post_id,它当前显示的结果是 POST_ID 3 Mysql 表: (`post_id`, `user_id`, `stat
谁能告诉我为什么我无法通过 post_id 从评论表中获得评论。 访问 token 是 [已编辑] post_id 是 100002553491860_419288394832940 帖子有一条评论,
所以我有不同用户的自定义帖子。 每个帖子都有以下元键: post_width , post_height以及常用数据,例如 title , description等如下图: $tags = $_POS
我有一个投票表,其中包含一个 post_id 列、一个 user_id 列和一个 vote 列。当有人为某个帖子投票时,会添加一行记录他们投票的帖子的 ID、他们的 user_id,并且 vote 列
我无法通过索引方法找到链接到特定帖子的出价。我已成功将我的出价链接到帖子(即我的出价表中有一个 post_id 列),但似乎无法“找到”它们。我目前收到错误消息“无法找到带有‘id’=”的出价 出价
我有一个类似下面的代码,我想在点击赞/评论/星时获取相应帖子的唯一post_id。 我添加了一个隐藏的输入标签来存储每个帖子的 post_id 的值,并在点击喜欢/评论/明星时尝试获取该值。但是我无法
所以我将使用带有帖子和评论的博客数据库模型的陈词滥调示例。 public class Post { public int Id { get; set; } public string Titl
我有一个评论系统,应该输入 id、idea_id、user_id、评论、数据和时间。一切似乎都有效,除了每次我发表评论时,idea_id 始终为 0。顺便说一句,一个想法基本上就是一个帖子。 我使用以
我有三个这样的表: // Posts_1 // Posts_2 // Posts_3 +----+---------+
我正在使用 laravel 5.5,我正在尝试向帖子添加评论,但在提交表单时出现以下错误 "SQLSTATE[23000]: Integrity constraint violation: 1048
WordPress 将帖子和帖子的特色图片视为两个单独的帖子,这似乎无法创建将帖子和帖子的图片 URL 放入同一行的 SELECT 查询,如下所示: post_id imageURL 100
我正在尝试使用 fb.ui 打开一个对话框,以便我的用户能够在他们的 friend 墙上发帖。我正在尝试 facebook 提供的最基本的示例: Post to Feed FB.init(
我是一名优秀的程序员,十分优秀!