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

时间:2020-08-15 19:20:00

标签: python-3.x django django-models django-views django-generic-relations

我花了很多时间来修复它,但未成功,所以我希望在这里能得到您的帮助。 我想做的是我想通过评论表单在详细信息页面上添加评论,我知道我的 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)

0 个答案:

没有答案