In django web apps csrf.js returning 404 , "detail": "CSRF Failed: CSRF token missing or incorrect."

Postbox 1 Reputation point
2021-06-25T12:17:18.197+00:00

Hi all,
Refer my application logs below, all the below files are throwing 404. But I can find these file in virtual environment via ssh.

2021-06-25T11:05:07.379075285Z Not Found: /static/rest_framework/js/jquery-3.5.1.min.js
2021-06-25T11:05:07.438874597Z Not Found: /static/rest_framework/css/default.css
2021-06-25T11:05:07.487314611Z Not Found: /static/rest_framework/css/prettify.css
2021-06-25T11:05:07.538870153Z Not Found: /static/rest_framework/js/ajax-form.js
2021-06-25T11:05:07.576363173Z Not Found: /static/rest_framework/js/csrf.js
2021-06-25T11:05:07.644366155Z Not Found: /static/rest_framework/js/bootstrap.min.js
2021-06-25T11:05:07.682412381Z Not Found: /static/rest_framework/js/prettify-min.js
2021-06-25T11:05:07.727584367Z Not Found: /static/rest_framework/js/default.js
2021-06-25T11:05:07.816163725Z Not Found: /static/rest_framework/js/jquery-3.5.1.min.js
2021-06-25T11:05:07.873995020Z Not Found: /static/rest_framework/js/ajax-form.js
2021-06-25T11:05:07.946735842Z Not Found: /static/rest_framework/js/csrf.js
2021-06-25T11:05:08.007143459Z Not Found: /static/rest_framework/js/bootstrap.min.js
2021-06-25T11:05:08.070873704Z Not Found: /static/rest_framework/js/prettify-min.js
2021-06-25T11:05:08.131653725Z Not Found: /static/rest_framework/js/default.js

Due to this issue , I am unable to pass CSRF token to post/put operation

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,956 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,664 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ajkuma 22,521 Reputation points Microsoft Employee
    2021-06-28T19:50:33.65+00:00

    @Postbox , Apologies for the delay in responding here.

    Oryx runs manage.py collectstatic on your behalf unless you specify the DISABLE_COLLECTSTATIC env var. Make sure you've set STATIC_ROOT in settings.py:

    // settings.py

      STATIC_ROOT = './static/'  
    Or having STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  
    

    To host static files in your web app, add the whitenoise package to requirements.txt and the configuration for it to settings.py.

    Note: It will depend on the Django version, always try the latest version.

      # requirements.txt  
      whitenoise==4.1.2  
    

    You can find a Django working sample with static files for Web Apps On Linux here.

    Kindly check this doc for the recommended configuration -https://github.com/microsoft/Oryx/wiki/Django-Tips

    Please let us know how it goes and I'll follow-up further.