Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch static CSS files with django on IIS?

Tags:

django

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

like image 201
Yura Avatar asked Aug 31 '15 13:08

Yura


People also ask

How does Django find static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

How do I enable static content in IIS?

In Control Panel, click Programs, and then click Turn Windows features on or off. In the Windows Features dialog box, click Internet Information Services, and then click OK. This action installs the IIS 8 default features. Install only the default features for a static-content web server.

Where are Django CSS files stored?

css . In other words, your stylesheet should be at polls/static/polls/style. css . Because of how the AppDirectoriesFinder staticfile finder works, you can refer to this static file in Django as polls/style.


2 Answers

Yes, I am a newbie. Sometimes I ask "newbie questions", same as many others here. But apparently, there is no room for such questions on this site (and this is why I got down-voted on my initial question at the top and got banned out of this site - this is my last answer that I can write on this site). Guys, people ask questions because they honestly ask for help and hope to get it here. They don't do it to irritate any of you (you can vote to close this answer too, if it makes you feel good)


Anyway, 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:

<?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>

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

5) run IIS server and enter your website. It should work.

like image 126
Yura Avatar answered Oct 16 '22 16:10

Yura


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.

like image 20
Lucas Menezes Guzzo Avatar answered Oct 16 '22 14:10

Lucas Menezes Guzzo