32

I have django installed on IIS8. Now I want to configure it to serve static files. I have been following this tutorial

I have added this to settings.py

STATIC_URL = '/static/'

STATIC_ROOT = 'C:/my_django_project/NZtracker/static_for_production/'

STATICFILES_DIRS = ( 'C:/my_django_project/NZtracker/static/', )

and then run collectstatic.

As a result, all the static folders have been moved to C:/my_django_project/NZtracker/static_for_production/.

Next, I was trying to configure IIS to serve files from that folder but couldn't. (I have tried following this post, but it also did not work)

How can I fix it? Thanks

Community
  • 1
  • 1
Yura
  • 2,381
  • 8
  • 33
  • 44
  • 1
    Yes, as that page says, you need to configure IIS to serve the assets; Django won't do it. – Daniel Roseman Aug 31 '15 at 13:49
  • 1
    I don't really understand how you can claim to have read the documentation, but not configured the relevant settings or understood that Django does not serve assets in production. Please, go and read the [deploying static files](https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/) docs and come back with any specific questions. – Daniel Roseman Aug 31 '15 at 15:48
  • Thank you @DanielRoseman. Indeed, I read something else and did not read this. PLEASE SEE MY EDITED QUESTION. – Yura Sep 01 '15 at 10:05
  • 1
    What about that linked post did not work? What exactly is happening now? – Daniel Roseman Sep 01 '15 at 10:16
  • Seems that nothing happens regarding the CSS files - the server does not fetch them when I create a virtual dir called "static". – Yura Sep 01 '15 at 10:19

2 Answers2

62

After struggling for 2 days, for the sake of those who have the same problem, I wanted to share a step-by-step solution for (probably a common) issue.

Problem

You have started a django project on IIS and it is working perfectly on your localhost. Now, when deploying it to web-server, the static files (CSS, JS, images,..) are not fetched. You can read here and here , but in fact, you don't want all these configurations and copying files from one directory to another...

What you want is that your IIS server will fetch the static files from the static/ directory, just as the development server (the one you run with python manage.py runserver) did. enter image description here

Solution

  1. inside the static/ directory, create a new file called web.config (notice, you probably have another web.config file in upper directory - don;t touch it. just leave it as is). enter image description here

  2. Write the following as the content of this file:

  3. Go to your IIS server -> right click the site name -> Add virtual directory enter image description here

  4. in alias wrote "static" (This is a MUST). Then, navigate to the folder in the project where all the static files are

enter image description here

  1. run IIS server and enter your website. It should work.
DeadChex
  • 4,379
  • 1
  • 27
  • 34
Yura
  • 2,381
  • 8
  • 33
  • 44
  • 5
    Thank you. I was struggling with this issue and thanks to you I fixed it. I agree with you that this site is too aggressive against new people. Sometimes one can read the documentation and still not be able to fix the problem. And isn't this site about helping one another? If you are going to answer me that I should read documentation, I wouldn't bother to ask in the first place. Again @Yura, thank you for this. – plasmy Mar 08 '16 at 14:48
  • 2
    That's exactly what I was missing... Thanks! – M. Sabev Mar 31 '16 at 14:33
  • Also good for flask! Thanks for persisting, not getting run out by the ogres, and providing a clear solid answer so I didn't have to spend two days figuring this out. – Bob Jordan Aug 12 '17 at 07:22
  • Thanks. I also had to enable the StaticFileModule via Windows Features, see https://stackoverflow.com/questions/16644810/after-installing-win-8-pro-and-iis-8-missing-staticfilemodule – Joris Jun 16 '20 at 13:30
  • I found this virtual directory method in many articles. But didn't work for me. I got: This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false". Lucas's answer worked for me. – Zeeshan Jul 08 '20 at 09:37
  • Oh great Thanks soo much! your answer is so fantastic! Exactly in the point. I like your first comment. The same happend to me in this site – zgrizzly_7 Feb 12 '21 at 17:23
  • Thank you so much. I was struggling the whole day and this fixed the issue. – harshawij Dec 06 '21 at 08:25
21

Yura's answer is quite complete. I had another approach, thought.

Right click the website and go to add application, enter the alias(used 'static') and then enter the physical path to your static files folder. Press OK.
Now, the tricky part, in the application you created for the static folder, go to the handle mapping and delete the mapping to the FastCGI site handler. Enter your site, it works!

It's pretty similar to Yura's answer, but uses the IIS interface instead of writing a web.config file by yourself.

  • This worked perfectly for me without having to set up a new `web.config` file. – Jonnyishman Feb 26 '18 at 10:03
  • Thank you @Lucas. This worked perfectly with Python 3.6.6, Django 2.2.7 and Internet Information Services (IIS) 10.0. The above solution using a web.config file did not work. – DarkHawk Dec 02 '19 at 14:24
  • Works for me. By adding just the virtual directory, or even this one before removing FastCGI handler, the request was going to Django urls.py in which there was no entry for static. As soon as I removed FastCGI handler it worked. – Zeeshan Jul 08 '20 at 09:39
  • I added virtual directory and but Removing FastCgiModule gives below error "HTTP Error 404.4 - Not Found The resource you are looking for does not have a handler associated with it" – kulvir Feb 14 '21 at 16:29