1
Vote

URL rewriting rules for Django

description

Hi there,
I am having some issues with the Django deployment on IIS. I followed the guide https://pytools.codeplex.com/wikipage?title=wfastcgi
with few differences when setting the handler path:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings>
        <add key="WSGI_HANDLER" value="django.core.handlers.wsgi.WSGIHandler()" />
        <add key="DJANGO_SETTINGS_MODULE" value="helloworld0.settings" />   
        <!-- <add key="PYTHONPATH" value="X:\Web\mywebsite\" /> -->
    </appSettings>
    <system.webServer>
        <handlers>
            <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\dev\python\python.exe|C:\dev\python\Scripts\wfastcgi.py" resourceType="Either" requireAccess="Script" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="Django mapping" stopProcessing="true">
                    <match url=".*" />
                    <action type="Rewrite" url="/" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
In particular the mapping "handler.fcgi" does not work for me (I have * instead). The problem I am facing now is that I cannot retrieve the static files from the server. For instance I can see the HTML response from Django for http://localhost:8080/admin/, but not the associated CSS files. E.g. /static/admin/css/base.css

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8080/static/admin/css/base.css

Using the URLconf defined in helloworld0.urls, Django tried these URL patterns, in this order:

    ^admin/

The current URL, static/admin/css/base.css, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I created a folder /static/ and executed manage.py collectstatic as described in https://docs.djangoproject.com/en/dev/howto/static-files/
However I do not understand how to perform the mapping for the static files in IIS. I tried to create a virtual folder and virtual application in IIS, or even changed the reg exp to exclude /static/, still the request is processed by Django, which returns the same error.
The application works fine with the Django embedded server.
Any hint ? :)

Kind regards,
  • Dean

comments

holyoaks wrote Sep 10 at 3:13 PM

I had this same problem. This is actually an IIS specific task, and what you have to do is add a condition to your rewrite rule. That condition should specify that your rule should run only when the condition is false, or negated in the IIS terminology.

My rewrite section is shown below:
   <rewrite>
       <rules>
        <rule name="Django Processing" >
           <match url="(.*)" ignoreCase="false" />
           <conditions>
             <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
           </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="false" />
         </rule>
       </rules>
     </rewrite>
The added condition specifies that the rewrite should occur only if the pattern is NOT matched.

As for the handler.fcgi, I couldn't understand that aspect of it either, and I still don't. I asked a question here, but have yet to receive an answer. I had to have the handler.fcgi for all of my routes to work, which doesn't make any sense to me.

holyoaks wrote Sep 10 at 3:15 PM

Forgot to mention you need to have a separate web.config in the static folder as well, that clears the handlers section and then specifies the handler for the static files. My web.config is shown below.
<?xml version="1.0" encoding="UTF-8"?>
Add a comment to this line
<configuration>
    <system.webServer>
      <handlers>
        <clear />
        <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      </handlers>
    </system.webServer>
</configuration>