Opened 19 years ago

Closed 18 years ago

Last modified 17 years ago

#428 closed enhancement (fixed)

[patch] view for serving static files

Reported by: volsung@… Owned by: Jacob
Component: Generic views Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be nice to be able to serve static files from disk while using the test django server. This can be done with a view and urlpattern.

I've hacked up an implementation at: http://django.pastebin.com/347411
To use it, put that in your view directory (say in static.py), then add

  DOCUMENT_ROOT = '/your/local/document/root/dir'

to your settings file, and a pattern:

  (r'^(?P<path>.*)$', 'foo.apps.foo.views.static.serve')

at the end of your urlpatterns list. This will pick up all the URLs that all the previous patterns couldn't match and try to find them under DOCUMENT_ROOT.

Is there any interest in adding this view to the other views django already provides?

Attachments (2)

static.py (1.8 KB ) - added by volsung@… 19 years ago.
static file view
static_view.patch (2.1 KB ) - added by Stan Seibert <volsung@…> 19 years ago.
Static file view in the form of a patch against django trunk

Download all attachments as: .zip

Change History (7)

by volsung@…, 19 years ago

Attachment: static.py added

static file view

by Stan Seibert <volsung@…>, 19 years ago

Attachment: static_view.patch added

Static file view in the form of a patch against django trunk

comment:1 by Stan Seibert <volsung@…>, 19 years ago

Summary: view for serving static files[patch] view for serving static files

Resubmitted static file view in the form of a patch. DOCUMENT_ROOT is still the same, but now your URL handler would look something like:

urlpatterns = patterns('',
    # Other patterns here
    (r'^(?P<path>.*)$', 'django.views.static.serve')  # This should be last
)

comment:2 by John Madson <jmadson@…>, 19 years ago

This is great. Please commit it, Djangods!

comment:3 by volsung@…, 19 years ago

priority: lownormal
Severity: minornormal

This is seems to be a really common request on IRC. Probably more than just low priority.

comment:4 by Jacob, 18 years ago

Resolution: fixed
Status: newclosed

(In [683]) Fixed #428 -- added a "static pages" view. Note that this view should only be used for testing!

comment:5 by Esaj, 18 years ago

There's a typo in the URL pattern example (an extraneous `)')

Index: django/views/static.py
===================================================================
--- django/views/static.py      (revision 699)
+++ django/views/static.py      (working copy)
@@ -13,7 +13,7 @@

     To use, put a URL pattern like::

-        (r'^(?P<path>).*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})
+        (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})

     in your URL conf; you must provide the ``document_root`` param.  You may
     also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
Note: See TracTickets for help on using tickets.
Back to Top