403 Error In Django

Hello! I am trying to implement dhtmlxgantt in a django project but am consistently getting 403 errors when making PUT, POST and DELETE requests in the frontend. Hoping someone can help with this.

For context, I want to use the gantt api in a django project with a few other applications. I initially included the gantt views and urls in one of the other apps. When I did this I could view tasks and links, but immediately got 403 errors when trying to add tasks and links from the frontend. Details of the error showed that this was related with a missing or invalid csrf token. I tried disabling csrf middleware and adding csrf exemption decorators to the gantt views, but this didn’t work. I tested the endpoints by sending data using forms I created in my html and this worked fine, showing that the problem was elsewhere.

Afterwards, I thought there might be server settings or other issues causing the problem, so I decided to create a new project. I started the project by building a standalone gantt app, following the dhtmlxGantt with Python Gantt Docs tutorial. The standalone gantt app worked perfectly after which I brought in code for the other apps into my project (mainly templates, views and urls). The gantt app still worked well as long as I used it with urls within that app. However, if I redirect to other apps e.g. to an app handling authentication, and back into the gantt again, the 403 errors start and persist.

I wonder whether anyone else has faced similar issues and can help with ideas to resolve this. Also not sure whether this is by design - maybe the api cannot be used in projects that require redirecting to/from other applications. Would appreciate thoughts and assistance.

Hello Mal,
Gantt is a client-side library. It doesn’t directly communicate with the database or the server-side. All changes occur only inside the browser.

To load the data, you can use the gantt.load or gantt.parse methods:
https://docs.dhtmlx.com/gantt/api__gantt_load.html
https://docs.dhtmlx.com/gantt/api__gantt_parse.html

To send the changes to the server, you need to use the Data Processor or built-in Ajax module. You can read more about it in the following articles:
https://docs.dhtmlx.com/gantt/desktop__server_side.html
https://docs.dhtmlx.com/gantt/api__gantt_ajax_other.html

In the default configuration, Gantt doesn’t use any custom headers including CSRF tokens. You can specify them in the headers parameter if you use the Data Processor:
https://docs.dhtmlx.com/gantt/desktop__server_side.html#customrequestheadersandparameters

Or you can manually send the requests to the server and specify any data and headers you need:
https://docs.dhtmlx.com/gantt/desktop__server_side.html#customrouting

Hi Ramil,

Thank you for the advice and resources. They were helpful in my troubleshooting.

It turns out the issue was my Django REST Framework authentication classes. I added these and everything seems to work well now. Pasting the settings below in case someone may face something similar in the future.

REST_FRAMEWORK = {
‘DEFAULT_AUTHENTICATION_CLASSES’: [
‘rest_framework.authentication.TokenAuthentication’,
],
}

INSTALLED_APPS += [
‘rest_framework.authtoken’,
]

Mal