【问题标题】:Django Comment Framework, Delete not workingDjango 评论框架,删除不起作用
【发布时间】:2015-08-05 19:58:10
【问题描述】:

我是 Django 新手。我目前正在尝试为我的应用程序中的每个帖子实现 cmets 部分。尝试删除特定评论时出现错误,并且错误 int() 参数必须是字符串或数字,而不是“SimpleLazyObject”。这是我的代码。

comment.html:

<h3> Comments </h3>
{% get_comment_count for post as comment_count %}
<p>This post has {{ comment_count }} comments.</p>

{% get_comment_list for post as comment_list %}

{% for comment in comment_list %}
    <div style="border: 1px solid green;">
    <a name="c{{ comment.id }}"></a>

    <a href="{% get_comment_permalink comment %}">
        permalink for comment #{{ forloop.counter }}
    </a>

    <hr>
    <h4> Just for testing purpose (Ignore) </h4>
    <p> content_object : {{comment.content_object.id}}</p>
    <p> content_type  : {{comment.content_type}}</p>
    <p> object_pk(Foreign Key) : {{comment.object_pk}} </p>
    <p> user(Foreign Key) : {{comment.user}}</p>
    <p> Comment ID : {{comment.id}}</p>
    <hr>
    <p>Posted by: {{ comment.user_name }} on {{ comment.submit_date }}</p>
    <p> {{comment.comment}}</p>

    <!-- NOT  SURE HOW WILL IT WORK -->
    <a href = '{% url 'delete_own_comment' comment.id %}'> Delete Comment </a>
  </div>
{% endfor %}

Views.py

import django_comments
from django_comments.models import Comment
from django_comments.views.moderation import perform_delete

def delete_own_comment(request, comment_id):
    comment = get_object_or_404(Comment, id=comment_id)
    # if comment.user.id != request.user.id:
    #   raise Http404
    perform_delete(request, comment)    

但是,我注意到,如果我在 get_object_or_404 的参数中给出任何错误的 comment_id,它会让我进入 404 页面,但是当传递正确的 comment_id 时,它会给我错误

Request Method:     GET
Request URL:    http://localhost:8080/posts/comments/delete/2/
Django Version:     1.8.2
Exception Type:     TypeError
Exception Value:    

int() argument must be a string or a number, not 'SimpleLazyObject'

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py in get_prep_value, line 985
Python Executable:  /usr/bin/python
Python Version:     2.7.6

2) 此外,在 html 中,评论链接的永久链接不起作用。它也给了我 404 错误:

post objects don't have a get_absolute_url() method

但是,我的帖子模型中确实有 get_absolute_url():

def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.id})

url.py

 # ex: /posts/3/
    url(r'^(?P<post_id>[0-9]+)/$', views.detail, name='post-detail'),

【问题讨论】:

    标签: python django django-comments


    【解决方案1】:

    我不在我的工作笔记本电脑前,但这应该可以工作:

    comment = get_object_or_404(YourCommentModel.id, id=comment_id) #assuming the pk for Comment is id
    

    另外,请确保您正在调用评论并将其分配给您的模型,例如

    YourCommentModel = models.ForeignKey(Comment)
    

    我可能错了,但您从未将 django_comment 分配给模型(或未将模型包含在问题中),这是必要的,否则该表不会添加到您的数据库中

    【讨论】:

    • 那不行。 Comment 是自动绑定到对象的 django 框架。我只需要弄清楚修改是如何工作的。请需要帮助。不过,感谢您的时间。我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多