Django on Azure not loading static files

2019-04-20 13:56发布

I followed the tutorial below to create a django project on azure:

http://www.windowsazure.com/en-us/develop/python/tutorials/web-sites-with-django/

Everything worked fine until I tried to install the django_admin_bootstrapped app. Now all static requests return 404 error. I don't if the new app caused the problem or just exposed it.

I have this:

STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    #os.path.join(PROJECT_DIR, 'site-packages/django_admin_bootstrapped/static'),
)

I read quite a bit online and some people say you have to configure your server to serve static files. I don't have direct access to the server, it's an azure website and I deploy through git. And like I said, the admin and everything used to work before I tried to install these apps that bootstrap the admin. Now even when the app is not under INSTALLED_APPS I don't get any css or js files.

Thanks!

4条回答
趁早两清
2楼-- · 2019-04-20 14:06

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.

查看更多
女痞
3楼-- · 2019-04-20 14:08

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!!!).

查看更多
We Are One
4楼-- · 2019-04-20 14:09

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!

查看更多
倾城 Initia
5楼-- · 2019-04-20 14:11

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" %}.

查看更多
登录 后发表回答