1. Chris Wesseling
  2. django-wsgiserver


Chris Lee-Messer  committed 821d88d

add more static files and a dummy app for testing django staticfiles support

  • Participants
  • Parent commits 5e61439
  • Branches default

Comments (0)

Files changed (16)

File .hgignore

View file

File README.rst

View file
-- 0.7.0 added support for serving static files directory by default if
+- 0.7.0 target django 1.3
+        added support for serving static files directory by default if
 	STATICFILES_ROOT is defined. Added more tests: ssl, staticfiles and
 	started to document them.  Experimental: STATICFILES_DIRS during
-	development to avoid needing to collect files all. Added django-odeon's
+	development to avoid needing to collect all files. Added django-odeon's
 	patch to wsgiserver to make it robust to illegal header lines
 - 0.6.10 add path for django.contrib.admin to address issue #5,#6
 - 0.6.9 typo fix

File django_wsgiserver/management/commands/runwsgiserver.py

View file
 'server_group': 'www-data',
 'ssl_certificate': None,
 'ssl_private_key': None,
+# options useful for development - should these all be true by default or off by default?
 'autoreload' : False,
 'adminserve' : True,  # please serve the admin media too by default
 'staticserve' : True, # serve static
-'servestaticdirs': False, # will use STATICFILES_DIRS and actuall serve each directory so
+'servestaticdirs': True, # will use STATICFILES_DIRS and actuall serve each directory so
                           # you don't need to collect them all in development
     except KeyboardInterrupt:
-def process_staticfiles_dirs(staticfiles_dirs):
+def process_staticfiles_dirs(staticfiles_dirs, default_prefix='/static/'): # settings.STATIC_URL
+    """
+    normalizes all elements of STATICFILES_DIRS to be of ('prefix','/root/path/to/files') form
+    the prefix gets added after STATIC_URL
+    """
     for staticdir in staticfiles_dirs:
+        # elements of staticfiles_dirs are ether simple path strings like "/var/www/polls/static"
+        # or are tuples/
         if isinstance(staticdir, (list, tuple)):
             prefix, root = staticdir
+            root = os.path.abspath(root)
-            prefix = ''
-            root = staticdir
+            # default url prefix used for single path elements
+            root = os.path.abspath(staticdir)
+            prefix = os.path.join(default_prefix, os.path.basename(root))+r'/' # end with /
         staticlocations.append((prefix, root))
     return staticlocations
        in the future I may need to adapt this to use a server.ssl_adapter
+    # debug
+    # print "options:"
+    from pprint import pprint; pprint(options)
     if options['daemonize'] and options['server_user'] and options['server_group']:
         #ensure the that the daemon runs as specified user
         path[settings.ADMIN_MEDIA_PREFIX] = mediahandler.MediaHandler(        
             os.path.join( django.contrib.admin.__path__[0], 'media'))
     if options['staticserve']:
-            path[settings.STATIC_URL] = mediahandler.MediaHandler(settings.STATIC_ROOT)        
+            if settings.STATIC_URL:
+                path[settings.STATIC_URL] = mediahandler.MediaHandler(settings.STATIC_ROOT)
+            else:
+                print "****"
+                print "STATIC_URL and STATIC_ROOT  must be set in settings file if wsgiserver option staticserve is set to True"
+                print "****"
         except AttributeError, msg:
             print msg
             print "****"
             print "STATIC_URL and STATIC_ROOT  must be set in settings file"
             print "****"
+        if settings.STATICFILES_FINDERS:
+            # debug
+            print "see settings.STATICFILES_FINDERS"
+            pprint (settings.STATICFILES_FINDERS)
+            from django.contrib.staticfiles.finders import AppDirectoriesFinder
+            app_static_finder=AppDirectoriesFinder(settings.INSTALLED_APPS)
+            print "app_static_finder.storages:"
+            pprint(app_static_finder.storages)
+            for key,val in app_static_finder.storages.items():
+                print key, " static location:", val.location
+            # need to decide what to do with this in terms of the fusion of the app static directories
         if options['servestaticdirs'] and hasattr(settings, 'STATICFILES_DIRS'):
             staticlocations = process_staticfiles_dirs(settings.STATICFILES_DIRS)
-            # print staticlocations
+            # debug !!!
+            from pprint import pprint; print "staticlocations:"; pprint(staticlocations)
             for urlprefix, root in staticlocations:
                 path[os.path.join(settings.STATIC_URL, urlprefix)] =  mediahandler.MediaHandler(root)
+    # debug
+    from pprint import pprint; print "apps:"; pprint(path)
     dispatcher =  WSGIPathInfoDispatcher( path )
     server = CherryPyWSGIServer(

File tests/testdjangoproject/dummyapp/__init__.py

Empty file added.

File tests/testdjangoproject/dummyapp/models.py

View file
+from django.db import models
+# Create your models here.

File tests/testdjangoproject/dummyapp/static/dummyapp/dummyapp_in_sub.txt

View file
+a test of dummyapp/static/dummyapp static file 

File tests/testdjangoproject/dummyapp/static/dummyapptxt.txt

View file
+this file is in dummyap/static/dummyapptxt.txt

File tests/testdjangoproject/dummyapp/tests.py

View file
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+Replace this with more appropriate tests for your application.
+from django.test import TestCase
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File tests/testdjangoproject/dummyapp/views.py

View file
+# Create your views here.

File tests/testdjangoproject/settings.py

View file
     # Uncomment the next line to enable the admin:
+    'django.contrib.admindocs',
+    'dummyapp',
 INTERNAL_IPS = ('localhost',

File tests/testdjangoproject/settings_statics.py

View file
+# this is updated to target the new static file handling in django 1.3
+from settings import *
-from settings import *
-STATICFILES_ROOT = os.path.join(PROJECT_ROOT, 'static')
-STATICFILES_URL = r'/static/'
+STATIC_ROOT = os.path.join(PROJECT_ROOT, 'site_static') # where files will be collected
+# staticfiles_urlpatterns your STATIC_URL setting can't be empty or a full URL, such as http://static.example.com/.
+STATIC_URL = r'/static/'
 # experimental for development work it would be nice to not need to
 # run collectstatic all the time
+# each app by default will have its app/static directory added, by convention these are usually subdirectories
+# app1/static/app1
+# app2/static/app2
+# but there is a problem if this convention isn't followed
+INSTALLED_APPS +=  ('django.contrib.staticfiles',)
   # ("urlprefix", "/path/to/the/files"), # I usually have urlprefix be appname
     os.path.join(PROJECT_ROOT, "static2"),
-    ('static3', os.path.join(PROJECT_ROOT, "notstatic3")),
+# don't remember what I was doing here with this tuple
+#    ('static3', os.path.join(PROJECT_ROOT, "notstatic3")),
+# List of finder classes that know how to find static files in
+# various locations.
+    'django.contrib.staticfiles.finders.FileSystemFinder',
+    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+#    'django.contrib.staticfiles.finders.DefaultStorageFinder',

File tests/testdjangoproject/static2/static2textfile.txt

View file
+This textfile is in the static2 directory

File tests/testdjangoproject/static2/substatic2/sub_textfile.txt

View file
+a text file that lives in static2/substatic2/sub_textfile.txt

File tests/testdjangoproject/templates/hello.html

View file
       <li> <a href="/admin/">Check the default admin css media service</a></li>
+      </ul>
+{% if 1 %}
+      <h4> if static file serving enabled </h4>
+      <ul>
       <li> <a href="/static/textfile.txt">/static/textfile.txt </a></li>
       <li> <a href="/static/htmlfile.html">/static/htmlfile.txt </a></li>
+      <li> <a href="/static/static2/textfile2.txt"> from static2/textfile2.txt mapped back to static </a></li>
+      <li> <a href="/static/static2/substatic2/sub_textfile.txt"> static2/substatic2/sub_textfile.txt </a></li>
       <li> <a href="/static/../settings.py"> /static/../settings.py security test</a> should not return the settings.py file! (either forbidden or remap to another url)</li>
       {% else %}
       <li> Staticfiles are not being used </li>
 {% endif %}

File tests/testdjangoproject/test_staticfiles

View file
 python ./manage.py runwsgiserver --settings=settings_statics

File tests/testdjangoproject/urls.py

View file
+#from django.contrib.staticfiles.urls import staticfiles_urlpatterns
+#from pprint import pprint
+#print "staticfiles_urlpatterns():"
+#pprint (staticfiles_urlpatterns())