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

199 Views Asked by At

I am getting an error while using the path in Django.

this is the code where the redirect function is called in 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 error:

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

I have also registered the app name in settings.py

1

There are 1 best solutions below

0
On BEST ANSWER

Change the redirect('chatapp:chat') to redirect('chat').

Explanation: The redirect function would search for the name of the URL in urls.py and in your case it's

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