属性错误:模块'post.views'没有属性'add_comment_to_post'。

问题描述 投票:0回答:1

我是python新手,在编辑views.py时,在我的djang项目中添加注释选项时,显示输入

python3 manage.py runserver

终端显示如下。

文件 "homeuserDocumentsDJANGO-COURSE-2.xxDJANGO_COURSE_2.xx21-Social_Clone_Projectsimplesocialpostsurls.py",第12行,在path('post/comment', views.add_comment_to_post, name='add_comment_to_post')中,AttributeError: 模块'post.views'没有属性'add_comment_to_post'。

以及 views.py 和 urls.py 文件。

image description is of view.py file

image description of urls

python django django-views django-urls python-django-storages
1个回答
1
投票

问题是你的函数缩进不正确。

add_comment_to_post 目前是之前定义的类的一部分(与具有函数的 delete 例如)。)

因此,如果你改变你的缩进方式,错误就会消失,如


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):
    ....
© www.soinside.com 2019 - 2024. All rights reserved.