如何解决使用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 举报,一经查实,本站将立刻删除。