使用GenaricForeignKey在DetailView中添加注释功能

问题描述

我花了很多时间来修复它,但未成功,所以我希望在这里能得到您的帮助。 我想做的是我想通过评论表单在详细信息页面添加评论,我知道我的 post 方法不完整,我应该在此方法添加什么,这样我才能发布评论。 我正在使用ContentTypes框架使其可重用。

评论/模型.py

<head>
  <Meta charset="utf-8" />
  <Meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no,user-scalable=0" />
  <Meta http-equiv='cache-control' content='no-cache'>
  <Meta http-equiv='expires' content='0'>
  <Meta http-equiv='pragma' content='no-cache'>

评论/forms.py

class GlobalComment(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    body = models.TextField()
    published = models.DateTimeField(auto_Now_add=True)

    content_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type','object_id')  # by default it will take these two parameters if u dont provide

    objects = GlobalCommentManager()

    def __str__(self):
        return self.body[:10]

myapp / views.py

class GlobalCommentForm(forms.Form):
    content_type = forms.CharField(widget=forms.HiddenInput)
    object_id    = forms.IntegerField(widget=forms.HiddenInput)
    body         = forms.CharField(widget=forms.Textarea)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)