【发布时间】: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