【问题标题】:what is key difference between posting a new comment and replying to existing comments?发布新评论和回复现有评论之间的主要区别是什么?
【发布时间】:2021-11-28 00:52:21
【问题描述】:

我想找出组合 comment form 时发布新评论和回复现有 cmets 的主要区别以及它们在 django_comments_xtd 的源代码中是如何实现的强>?

我看了很长时间的源代码,没有找到答案。

【问题讨论】:

  • 回复时,系统将原始评论帖子ID记录在数据库中,以便与新插入的回复评论帖子链接
  • 感谢您的回复。但是请详细说明它们是如何在源代码中实现的?
  • 请参考我的回答。

标签: django-comments


【解决方案1】:

在 django_cmets_xtd 中,系统通过引用 reply_to 数据字段来区分评论是对先前帖子的回复/还是新的独立评论。

对于新评论,reply_to 字段的默认值为“0”,但对于回复之前的评论(例如 ID=14),reply_to 数据字段的值为“14”。

对于源代码,您可以参考的地方之一是 python 文件 forms.py(下面附有摘录)。当然还有其他地方使用这个 reply_to 数据字段来呈现网站,因此您可以搜索 reply_to 字符串来发现您是否对这方面有进一步的兴趣。

class XtdCommentForm(CommentForm):
    followup = forms.BooleanField(required=False,
                                  label=_("Notify me about follow-up comments"))
    reply_to = forms.IntegerField(required=True, initial=0,
                                  widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        comment = kwargs.pop("comment", None)
        if comment:
            initial = kwargs.pop("initial", {})
            initial.update({"reply_to": comment.pk})
            kwargs["initial"] = initial
            followup_suffix = ('_%d' % comment.pk)
        else:
            followup_suffix = ''

        super(CommentForm, self).__init__(*args, **kwargs)

        self.fields['name'].label = _("Name")
        self.fields['name'].widget = forms.TextInput(
            attrs={'placeholder': _('name'), 'class': 'form-control'})

        self.fields['email'].label = _("Mail")
        self.fields['email'].help_text = _("Required for comment verification")
        self.fields['email'].widget = forms.TextInput(
            attrs={'placeholder': _('mail address'), 'class': 'form-control'})

        self.fields['url'].label = _("Link")
        self.fields['url'].required = False
        self.fields['url'].widget = forms.TextInput(attrs={
                'placeholder': _('url your name links to (optional)'),
                'class': 'form-control'})

        self.fields['comment'].widget = forms.Textarea(
            attrs={'placeholder': _('Your comment'), 'class': 'form-control'})
        self.fields['comment'].max_length = settings.COMMENT_MAX_LENGTH
        self.fields['comment'].widget.attrs.pop('cols')
        self.fields['comment'].widget.attrs.pop('rows')

        self.fields['followup'].widget.attrs['id'] = (
            'id_followup%s' % followup_suffix)
        self.fields['followup'].widget.attrs['class'] = "custom-control-input"
        self.fields['followup'].initial = settings.COMMENTS_XTD_DEFAULT_FOLLOWUP

    def get_comment_model(self):
        return TmpXtdComment

    def get_comment_create_data(self, site_id=None):
        data = super(CommentForm, self).get_comment_create_data(site_id=site_id)
        ctype = data.get('content_type')
        object_pk = data.get('object_pk')
        model = apps.get_model(ctype.app_label, ctype.model)
        target = model._default_manager.get(pk=object_pk)
        data.update({'thread_id': 0, 'level': 0, 'order': 1,
                     'parent_id': self.cleaned_data['reply_to'],
                     'followup': self.cleaned_data['followup'],
                     'content_object': target})
        return data

【讨论】:

  • 感谢您的详细回答。我的问题现在已经完全回答了。但是如果你能进一步分享一些见解,当用户点击“回复”回复现有评论时,如何直接弹出回复对话框(换句话说,生成正确的form内容)? 因为我不希望用户被重定向到另一个 URL 进行回复。我意识到我只需要将form = get_form()(comment.content_object, comment=comment) 作为额外的关键字传递到模板标签{% include comments/form.html %} 中,但是如果没有在django_comment_xtd 包中进行重大定制,我就无法弄清楚。
猜你喜欢
  • 1970-01-01
  • 2021-08-03
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2012-10-11
相关资源
最近更新 更多