简体   繁体   English

如何在 django 中用博客文章标记评论?

[英]How to tag a comment with the blog post in django?

i am new to django and am currently working on developing a blog.我是 Django 的新手,目前正在开发一个博客。

I am trying to insert a comment form for my viewers using the CreateView template.我正在尝试使用 CreateView 模板为我的观众插入评论表单。 Viewers will need to log in to type their comment.观众需要登录才能输入他们的评论。 Their username will be tagged to the comment for the respective blog without them needing to indicate anything.他们的用户名将被标记到相应博客的评论中,而无需指明任何内容。 There are no errors but data is not stored as expected.没有错误,但数据未按预期存储。 Comment is accepted but does not appear on the blog.评论被接受,但不会出现在博客上。 Went to check on django administration where apparently only the author is stored but not the blog post that comment is on.去检查 django 管理,其中显然只存储了作者,但没有存储评论的博客文章。 Below is my code that is stored in the (views.py).下面是我存储在 (views.py) 中的代码。

class CommentCreate(LoginRequiredMixin,CreateView):
    model = Comment
    fields = ['comment','blog']
    template_name = 'catalog/blog_comment.html'

    def get_context_data(self, **kwargs):
        context = super(CommentCreate, self).get_context_data(**kwargs)
        context['blog'] = get_object_or_404(Blog, pk = self.kwargs['pk'])
        return context

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super(CommentCreate, self).form_valid(form)

    def get_success_url(self): 
        return reverse('blog-detail', kwargs={'pk': self.kwargs['pk'],})

The models Blog and Comment is defined as below in a separate file (models.py)模型博客和评论在单独的文件(models.py)中定义如下

class Blog(models.Model):
    title = models.CharField(max_length=200)    
    blogger = models.ForeignKey('Blogger', on_delete=models.SET_NULL, 
null=True)  
    content = models.TextField(max_length=1000, help_text='Write some stuffs 
for your blog')
    post_date = models.DateField(default=date.today)

    class Meta:
        ordering = ['post_date',"title","blogger"]

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog-detail', args=[str(self.id)])


class Comment(models.Model):
    comment = models.TextField(max_length=1000, help_text='Comment on the 
 blog')
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    blog = models.ForeignKey("Blog", on_delete=models.SET_NULL, null=True)
    post_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-post_date']

    def __str__(self):
        return self.comment

Any advice is appreciated.任何建议表示赞赏。 Thank you.谢谢你。

Problem is solved.问题解决了。 Turns out i forgot to save the data in the form_valid.原来我忘了将数据保存在 form_valid 中。 Appended code is listed as below.附加代码如下所示。

class CommentCreate(LoginRequiredMixin,CreateView):
    model = Comment
    fields = ['comment','blog']
    template_name = 'catalog/blog_comment.html'

    def get_context_data(self, **kwargs):
        context = super(CommentCreate, self).get_context_data(**kwargs)
        context['blog'] = get_object_or_404(Blog, pk = self.kwargs['pk'])
        return context

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.blog = get_object_or_404(Blog, pk = self.kwargs['pk'])
        return super(CommentCreate, self).form_valid(form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM