找不到'add_comment_to_post'的反向符号。 'add_comment_to_post'不是有效的视图函数或模式名称

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

我是python的新手,同时在运行post.html文件的同时向我的djang项目添加注释选项时遇到此错误它说:

django.urls.exceptions.NoReverseMatch: Reverse for 'add_comment_to_post' not found. 'add_comment_to_post' is not a valid view f
unction or pattern name.

并且浏览器页面的屏幕截图是enter image description here和html代码是:enter image description here

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

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

[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.