static files in production

357 views
Skip to first unread message

Tony King

unread,
Nov 7, 2017, 12:21:32 PM11/7/17
to Django users

Hi again,

I'm having to deploy my project using IIS 8.5 as this all I have access too and I've been relatively successful, however, my static image files are not displaying in the way they did in the development environment.

I've tried adding what I believe to be the recommended settings but I'm still not getting any images too load.

In an almost desperate attempt to understand the search path logic, I've ended up with several static folders in my folder structure and still I do not have any images.

The attached file includes a screen shot of the settings.py and folder structure, which I'm hoping will be useful.

Can someone please help?

Kind regards,

  Tony

shopfront01.png

Dylan Reinhold

unread,
Nov 7, 2017, 1:20:05 PM11/7/17
to django...@googlegroups.com
In production django does not serve static files.
You need to configure iis to serve the files directly from your STATIC_ROOT directory. 
Then from your project you run manage.py collectstatic .
Django will collect up all the staic files and put them in STATIC_ROOT.

Dylan


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/eae9ab3b-8a2b-49ae-ae24-c837545af7ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonis Christofides

unread,
Nov 7, 2017, 2:10:38 PM11/7/17
to django...@googlegroups.com

Hello,

You might find my drawings on how django static files work in production helpful. Unfortunately they are only for nginx and apache, but the same principles apply to IIS.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Tony King

unread,
Nov 8, 2017, 3:57:58 AM11/8/17
to Django users

Thanks.  The diagram clarified what I'd already thought to a degree, although I'd gotten confused by the warning in the documentation for STATIC_ROOT, which says 

"
This should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).
"

 I've set the variables in my settings.py;
STATIC_ROOT = "C:/inetpub/wwwroot/shopfront/static/"
STATIC_URL = '/static/'

I've added 'django.contrib.staticfiles', to my INSTALLED_APPS
I've run the collectstatic and it did indeed put my handful of static files in \static\ grouped into their respective folders.

I've even added  the STATICFILES_FINDERS to my settings.py

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

When I view the source of my page, I can see the url built from "{% static my_apps.app_icon %}" resolves to /static/images/filename.png

To be sure, I wasn't going totally mad I added the following to my urls.py and re-enabled DEBUG

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and low and behold, it worked just fine but I obviously cannot leave it like that.

Having gone through the tutorial, I thought Django would be a great tool but I seem to be struggling with the most simple of things and I've barely started this project, I'm just trying to test out some basics including the deployment.

Kind regards,

  Tony

Tony King

unread,
Nov 8, 2017, 5:58:34 AM11/8/17
to Django users

Ok, I've found a solution here which allows the serving of static files from within the project file folder structure as gathered by collectstatic.  I now see that the FastCGI Handler required for the project would interfere with the serving of static files.

Although marginally less convenient, I've also found that putting the static files in a folder outside of the project folder and pointing STATIC_ROOT to that folder is also an option i.e.

\inetpub\wwwroot\projectfolder\
\inetpub\wwwroot\django-staticfiles\

STATIC_ROOT = 'C:/inetpub/wwwroot/DjangoProjects/static'



Reply all
Reply to author
Forward
0 new messages