[Solved]-Django on Azure not loading static files

17👍

I had this problem and none of the suggested answers seemed to fit. My bizarre solution was to switch off Python on the Azure web site configure page.

I arrived at this odd conclusion by installing the PTVS Django sample and following the steps in this tutorial http://azure.microsoft.com/en-us/documentation/articles/web-sites-python-ptvs-django-sql/. The only difference I could fine between my site and the working tutorial was that Django was off! If someone has an explanation I would love to hear it (PHP is enabled!!!).

5👍

I found my solution on this page: http://mrtn.me/blog/2012/06/27/running-django-under-windows-with-iis-using-fcgi/

I had to create a central static folder and add a web.config for iis to serve the files. web.config below:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- this configuration overrides the FastCGI handler to let IIS serve the static files -->
    <handlers>
    <clear/>
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>

Hope this helps someone!

0👍

I don’t have enough rep to vote up but the answer does in fact work on Azure for me as well, and appears to be, so far, the only method of doing it. Just remember that the web.config needs to be in the right place…lol…I apparently had multiple static folders as I was trying different ways of solving this.

0👍

If after trying all the other proposed solutions you still find yourself at trouble, you may have to understand that depending on the server that’s running you application is the way static files are server. Django has it’s own server, which is run by using the command:

python manage.py runserver

But PAAS providers do not use this server in most of the cases. gunicorn is in most times the chosen one. Azure sometimes uses IIS’s FastCGI handler but at current date it is intelligent enough to detect a django application and try to use django’s default server.

So, the first step you have to take is to find out what server is azure using to run your app, and you may know that by reading azure’s log:

https:// YOUR APP NAME.scm.azurewebsites.net/api/logstream

If you confirm that azure is using django’s default server, you must bear in mind that django does not server static files automatically when in a production environment. You must configure the static folder’s url. So in your config/urls.py set the static url, like this:

from django.conf import settings
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path

urlpatterns = i18n_patterns(path('admin/', admin.site.urls), prefix_default_language=False) + \
    static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
    static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

As you can see, I enable admin, static and media urls. This is enough to let django’s default server know where to find and serve static files (and media files too, as well as admin routes)

-1👍

Yes, I can confirm that the ‘right place’ to store your static files for a Django deployment on Azure Web Sites is the ‘static’ directory in the web root.

Step 1: create a directory called static in your web root (www root)

Step 2: edit Django’s settings.py and set STATIC_ROOT = 'D:/home/site/wwwroot/static'

Step 3: In your templates use the construct {% static "static_file_name" %}.

Leave a comment