Code

Opened 9 years ago

Closed 9 years ago

Last modified 7 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: UI/UX:

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@… 9 years ago.
static file view
static_view.patch (2.1 KB) - added by Stan Seibert <volsung@…> 9 years ago.
Static file view in the form of a patch against django trunk

Download all attachments as: .zip

Change History (7)

Changed 9 years ago by volsung@…

static file view

Changed 9 years ago by Stan Seibert <volsung@…>

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

comment:1 Changed 9 years ago by Stan Seibert <volsung@…>

  • Summary changed from view for serving static files to [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 Changed 9 years ago by John Madson <jmadson@…>

This is great. Please commit it, Djangods!

comment:3 Changed 9 years ago by volsung@…

  • priority changed from low to normal
  • Severity changed from minor to normal

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

comment:4 Changed 9 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:5 Changed 9 years ago by Esaj

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.