找不到 'xyz' 的反向。 'chat' 不是有效的视图函数或模式名称。在重定向('xyz')

Reverse for 'xyz' not found. 'chat' is not a valid view function or pattern name. At redirect('xyz')

我在 Django 中使用路径时遇到错误。

这是views.py

中调用重定向函数的代码
def addmsg(request):
    c = request.POST['content']
    new_item = messageItem(content = c)
    new_item.save()
    bot_msg(c)
    return redirect('chatapp:chat')

urls.py

app_name = 'chatapp'
urlpatterns = [
    path('chat', views.chatbot, name='chat'),
    path('addmsg',views.addmsg, name='addmsg'),
    ]

Django 错误:

NoReverseMatch at /addmsg
Reverse for 'chat' not found. 'chat' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://localhost:8000/addmsg
Django Version: 2.2.17
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'chat' not found. 'chat' is not a valid view function or pattern name.
Exception Location: C:\Users\xyz\Documents\Python\hotel\hotel2\env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 673
Python Executable:  C:\Users\xyz\Documents\Python\hotel\hotel2\env\Scripts\python.exe
Python Version: 3.9.1
Python Path:    
['C:\Users\xyz\Documents\Python\hotel\hotel2',
 'C:\Users\xyz\AppData\Local\Programs\Python\Python39\python39.zip',
 'C:\Users\xyz\AppData\Local\Programs\Python\Python39\DLLs',
 'C:\Users\xyz\AppData\Local\Programs\Python\Python39\lib',
 'C:\Users\xyz\AppData\Local\Programs\Python\Python39',
 'C:\Users\xyz\Documents\Python\hotel\hotel2\env',
 'C:\Users\xyz\Documents\Python\hotel\hotel2\env\lib\site-packages']
Server time:    Mon, 1 Feb 2021 18:26:38 +0000

我也在settings.py

注册了应用名称

将重定向('chatapp:chat')更改为重定向('chat')。

说明: 重定向功能将在 urls.py 中搜索 URL 的名称,在您的情况下是

path('chat', views.chatbot, name='chat'),