NWSGI - serving static files using wildcard mapping

Dec 28, 2012 at 12:31 PM
Edited Jan 3, 2013 at 9:29 AM

Hi,
I'm trying to create website using:
    IronPython 2.7.3
    Django 1.4.1
    NWSGI 2.1.0.10612
    IIS 7.5
   
So far everything works fine, except static files.
I tried: "...to move static files to a separate virtual directory or even separate server that does not use NWSGI.", and still getting 404 error.

ERROR: <title>IIS 7.5 Detailed Error - 404.0 - Not Found</title>

Web.config:
    <wildcard physicalPath="C:\inetpub\wwwroot\project\project\wsgi.py" />

settings.py:
    STATIC_ROOT = 'static'
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        '//remotedesktop/mystaticfiles',
    )
    TEMPLATE_CONTEXT_PROCESSORS = {
        'django.core.context_processors.static',
    }
    TEMPLATE_DIRS = (
        '//remotedesktop/mystaticfiles',
    )
   
url.py:
    urlpatterns = patterns('',
        url(r'^$', 'mysite.views.index'),
        url(r'^project/teststatic', 'mysite.views.teststatic'),
    )
    urlpatterns += staticfiles_urlpatterns()
 
views.py:
    def teststatic(request):
        return render_to_response('staticfiletest.html', {}, context_instance=RequestContext(request), mimetype="text/html; charset=utf-8" )

//remotedesktop/mystaticfiles
    img1.jpg
    staticfiletest.html   

staticfiletest.html:
    <!-- <img src="{{ STATIC_URL }}img1.jpg" /> -->
   
Can someone please help me to solve this ?

Thanks,
Dusan

Coordinator
Jan 1, 2013 at 2:01 AM

Just wanted to let you know that I have seen this but haven't had a chance to look into it yet. I'll try and dig into it as soon as I can.

Jan 9, 2013 at 9:58 AM

Hi Jeff,
There was a syntax error in my code.
My STATIC_URL should be as /project/static.

Now my textual static files works fine, but images are still broken.
"Unable to translate bytes [FF] at index 0 from specified code page to Unicode."

Coordinator
Jan 9, 2013 at 4:26 PM

I'm glad you figured it out, and I'm sorry for not getting back to you.

Your second problem, I bet, is a Django bug. IronPython's str()s, unlike CPython, are actually Unicode - to properly handle bytes, Django should be using the bytes() type. The stack trace should be able to give you an idea of where the image is being loaded and how, but it will mean digging into the Django code (which is actually quite nice).

Jan 10, 2013 at 11:35 AM
Edited Jan 10, 2013 at 12:07 PM

I've finaly made it!
You were right, there is a bug in Django's static.py.
I've simply replaced "response = HttpResponse(f.read(), mimetype=mimetype)" with
"response = HttpResponse(bytes(f.read()), mimetype=mimetype)", and it works now.

Thanks a lot for the hint, this have bothered me for a long time.

PS: as this is not nwsgi issue maybe this thread should be deleted

Coordinator
Jan 10, 2013 at 4:24 PM

Nah, the thread might still be useful to someone else, so I'll leave it.

If you're got some time you should file an issue with Django. They have no problem taking fixes for IronPython.