0

I am new to Django. I am currently trying to implement comments section for each post in my application. I am having error when trying to delete specific comment and getting error int() argument must be a string or a number, not 'SimpleLazyObject'. Here is my code.

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)    

However, I have noticed that if I give any wrong comment_id in argument for get_object_or_404, it gets me to 404 page, but when correct comment_id is passed, it gives me error

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) Furthermore in html, permanent link for comment link is not working. Its giving me 404 error too:

post objects don't have a get_absolute_url() method

However, I do have get_absolute_url() in my post model:

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'),
user3487775
  • 147
  • 9

1 Answers1

0

I'm not in front of my work laptop but this should work:

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

Also, make sure you are calling comment and assigning it to your model like

YourCommentModel = models.ForeignKey(Comment)

I may be wrong but you never assigned django_comment to a model (or didn't include the model in the problem) which is a necessity otherwise the table isn't added to your DB

Obj3ctiv3_C_88
  • 1,478
  • 1
  • 17
  • 29
  • That's not working. Comment is django framework that automatically bind to object. I just need to figure out how modifcation works in it. Need help please. Thanks for your time though. I really appreciate. – user3487775 Aug 08 '15 at 08:26