Search code examples
pythondjangodjango-viewsdjango-urlspython-django-storages

AttributeError: module 'posts.views' has no attribute 'add_comment_to_post'


i am new to python and while adding comment option to my djang project while editing the views.py its show while typing

python3 manage.py runserver

the terminal shows the following:

File "/home/user/Documents/DJANGO-COURSE-2.xx/DJANGO_COURSE_2.xx/21-Social_Clone_Project/simplesocial/posts/urls.py", line 12 , in path('post//comment/', views.add_comment_to_post, name='add_comment_to_post'), AttributeError: module 'posts.views' has no attribute 'add_comment_to_post'

and the views.py and urls.py file:

image description is of view.py file

image description of urls


Solution

  • The problem is an incorrect indentation of your function.

    add_comment_to_post is currently part of the previous defined class (with has the function delete for example).

    So if you change your indentation the error will be gone, e.g.

    
    class MyView(...):
        ...
        def delete(self, *args, **kwargs):
            messages.success(self.request, 'Post Deleted')
            return super().delete(*args, **kwargs)
    
    
    # next method should not have the same indentation of `delete`
    def add_comment_to_post(request, pk):
        ....