Django Backend on Azure App Service: Code only works when debug is set to True

Nick Mol 1 Reputation point
2024-01-03T12:06:24.56+00:00

I am deploying a Django app on Azure App Services. When Debug = True the application works perfect. I can view my APIs, navigate to different pages, and create and delete records in my database.

However, when debug = False I get an internal server error 500. I don't see any warnings in my application logs or the log stream. I already tried to change the allowed_hosts, but this does not seem to be the cause of the problem.

I just disabled the STATICFILES_STORAGE setting in my deployment settings. When I now run the code in debug = False it works. However, I assume I will need whitenoise to handle my static files....


#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

What is the best way to solve this? Or can I simply leave out Staticfiles_storage without consequences?

https://github.com/NickMol/Django-React-Deploy-Tutorial

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,407 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Konstantinos Passadis 17,456 Reputation points MVP
    2024-01-03T14:06:36.9733333+00:00

    Hello @Nick Mol !

    It's not recommended to leave out STATICFILES_STORAGE in your Django app, especially in a production environment. When DEBUG is set to True, Django serves static files itself, but in production (when DEBUG is set to False), you should use a production-ready static files solution like Whitenoise or Django's built-in collectstatic command to serve static files efficiently. If disabling STATICFILES_STORAGE makes it work, it might indicate an issue with your static files configuration. Review your static files setup, check file permissions, and make sure your static files are correctly collected and served with Whitenoise or another suitable storage backend.

    EXAMPLE :

    To collect static files in Django, follow these steps:

    1. Ensure that your STATIC_URL and STATIC_ROOT settings are correctly configured in your Django project's settings.
    # settings.py
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    1. Run the following command to collect static files:
    python manage.py collectstatic
    

    This command will gather all static files from your apps into the directory specified by STATIC_ROOT.

    1. Make sure your web server (e.g., Gunicorn or uWSGI) serves these static files correctly in production. You may use a middleware like Whitenoise or configure your web server to serve static files.

    By following these steps, your Django app will collect and serve static files in a production environment while DEBUG is set to False.

    Also , Verify that your ALLOWED_HOSTS setting includes the correct domain names or IP addresses.


    I hope this helps!

    The answer or portions of it may have been assisted by AI Source: ChatGPT Subscription

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


  2. brtrach-MSFT 15,791 Reputation points Microsoft Employee
    2024-01-04T01:12:20.7166667+00:00

    @Nick Mol You mentioned that disabling STATICFILES_STORAGE in your deployment settings resolved the issue, but you are concerned about not using WhiteNoise to handle your static files. You can still use WhiteNoise to serve your static files even if you disable STATICFILES_STORAGE.

    To do this, you can add the following code to your settings.py file:

    MIDDLEWARE = [
        # ...
        'whitenoise.middleware.WhiteNoiseMiddleware',
        # ...
    ]
    
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
    
    

    This will enable WhiteNoise to serve your static files even if STATICFILES_STORAGE is disabled. Make sure to install WhiteNoise by running pip install whitenoise in your virtual environment.

    Since you did not find any actionable errors in log stream, can you also try running your app locally with DEBUG set to False to see if you can reproduce the issue and debug it locally?


  3. Konstantinos Passadis 17,456 Reputation points MVP
    2024-01-04T05:04:03.7533333+00:00

    Hello @Nick Mol !

    Please verify that you have an Application Configuration Setting named

    WEBSITE_HOSTNAME

    and another MY_SECRET_KEY

    Sample

    configuration_azure_app_settings


    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    0 comments No comments

  4. Konstantinos Passadis 17,456 Reputation points MVP
    2024-01-04T13:21:33.4666667+00:00

    Hello @Nick Mol !

    Please have a look :

    https://stackoverflow.com/questions/46479378/valueerror-at-missing-staticfiles-manifest-entry-for

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards

    0 comments No comments