Hi everyone, I’m working on an application created with Django and I’ve run into a problem that is impossible for me to solve. I’m adding a chatbot to the web, more specifically the RASA chatbot, and the problem in a nutshell is that I want to make requests to Django endpoints, so that the corresponding function in the views.py
file is called and render or redirect to the desired page from the browser.
I can’t do this from the frontend itself because I generate the request from the Rasa Server, and I can’t return this information to the Frontend or read it from there, because the frontend connects to the Server through a Github widget (rasa-webchat) that connects through Sockets (socket.io), which doesn’t allow passing extra information through Sockets or accessing the Socket object that is connected to the Server.
I have managed to pass the Session cookie and the CSRF protection cookie to the Server, and I have added them to the requests I make from Rasa. I thought that would be enough, but it was not. Now I get it to call the endpoint and correctly detect the Django user, but I can’t get it to update the view in the Browser.
From what I have investigated the problem could be of the WSGIRequest object of Django, that is always passed as first parameter to all the functions of views.py
. This I believe that it is generated automatically with the request and I believe that with that object it knows to which Browser it must update the view if render()
or redirect()
is called.
To give you a better understanding, as an example, I tried to call the logout/
endpoint from Rasa:
class ActionLogout(Action):
def name(self) -> Text:
return "action_logout"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
requests.get(domainUrl + "logout/", params={"csrfmiddlewaretoken":tracker.get_slot("csrfmiddlewaretoken")},
cookies={"sessionid":tracker.get_slot("sessionid"),"csrftoken":tracker.get_slot("csrftoken")})
if (requests): # successful response
dispatcher.utter_message(text="The session has been successfully closed")
else:
dispatcher.utter_message(text="An error has occurred")
return []
Which is declared like this:
urlpatterns = [
path('admin/', admin.site.urls),
path('', index, name="home"),
# Users
path('login/', login_view, name="login"),
path('signup/', signup_view, name="signup"),
path('logout/', logout_view, name="logout"),
...
And it calls the following function:
def logout_view(request):
logout(request)
return redirect('home')
When calling the endpoint it accesses the logout function and executes it perfectly, but it does not redirect home
('home'
refers to index.html, and in the code it detects it correctly).
The endpoints are declared in the urls.py
file inside the Django Project and the views.py
are declared inside a Django App called DataVisualization, which is declared in the INSTALLED_APPS in settings.py of the project.
I understand that for it to work I need to pass some extra information to the request so that it knows which browser to go to. Can someone please help me? I’m really stuck…