mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
* Updated StaticFilesHandler and AdminMediaHandler to make use of the 404 handler if needed. * Updated runserver management command to serve static files only in DEBUG mode (or if specified the --insecure option) and if the staticfiles app is in INSTALLED_APPS. Also added an option to disable serving completely (--nostatic). * Added check in debug mode if STATICFILES_* settings are different to MEDIA_* settings. * Removed a faulty PendingDeprecationWarning in AdminMediaHandler that is triggered every time runserver is used. * Fixed an issue with the modification time checks when running collectstatic. * Extended and refined documentation. Thanks to everyone for input, especially to Carl Meyer, Ted Kaemming and Adam Vandenberg for patches. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14533 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -9,6 +9,10 @@ class Command(BaseCommand):
|
||||
option_list = BaseCommand.option_list + (
|
||||
make_option('--noreload', action='store_false', dest='use_reloader', default=True,
|
||||
help='Tells Django to NOT use the auto-reloader.'),
|
||||
make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
|
||||
help='Tells Django to NOT automatically serve static files at STATICFILES_URL.'),
|
||||
make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
|
||||
help='Allows serving static files even if DEBUG is True.'),
|
||||
make_option('--adminmedia', dest='admin_media_path', default='',
|
||||
help='Specifies the directory from which to serve admin media.'),
|
||||
)
|
||||
@@ -42,6 +46,8 @@ class Command(BaseCommand):
|
||||
use_reloader = options.get('use_reloader', True)
|
||||
admin_media_path = options.get('admin_media_path', '')
|
||||
shutdown_message = options.get('shutdown_message', '')
|
||||
use_static_handler = options.get('use_static_handler', True)
|
||||
insecure_serving = options.get('insecure_serving', False)
|
||||
quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
|
||||
|
||||
def inner_run():
|
||||
@@ -60,7 +66,11 @@ class Command(BaseCommand):
|
||||
|
||||
try:
|
||||
handler = WSGIHandler()
|
||||
handler = StaticFilesHandler(handler)
|
||||
allow_serving = (settings.DEBUG and use_static_handler or
|
||||
(use_static_handler and insecure_serving))
|
||||
if (allow_serving and
|
||||
"django.contrib.staticfiles" in settings.INSTALLED_APPS):
|
||||
handler = StaticFilesHandler(handler)
|
||||
# serve admin media like old-school (deprecation pending)
|
||||
handler = AdminMediaHandler(handler, admin_media_path)
|
||||
run(addr, int(port), handler)
|
||||
|
||||
@@ -651,12 +651,6 @@ class AdminMediaHandler(StaticFilesHandler):
|
||||
from django.conf import settings
|
||||
return settings.ADMIN_MEDIA_PREFIX
|
||||
|
||||
def __init__(self, application, media_dir=None):
|
||||
warnings.warn('The AdminMediaHandler handler is deprecated; use the '
|
||||
'`django.contrib.staticfiles.handlers.StaticFilesHandler` instead.',
|
||||
PendingDeprecationWarning)
|
||||
super(AdminMediaHandler, self).__init__(application, media_dir)
|
||||
|
||||
def file_path(self, url):
|
||||
"""
|
||||
Returns the path to the media file on disk for the given URL.
|
||||
@@ -666,13 +660,23 @@ class AdminMediaHandler(StaticFilesHandler):
|
||||
is raised.
|
||||
"""
|
||||
# Remove ``media_url``.
|
||||
relative_url = url[len(self.media_url):]
|
||||
relative_url = url[len(self.media_url[2]):]
|
||||
relative_path = urllib.url2pathname(relative_url)
|
||||
return safe_join(self.media_dir, relative_path)
|
||||
|
||||
def serve(self, request, path):
|
||||
document_root, path = os.path.split(path)
|
||||
return static.serve(request, path, document_root=document_root)
|
||||
def serve(self, request):
|
||||
document_root, path = os.path.split(self.file_path(request.path))
|
||||
return static.serve(request, path,
|
||||
document_root=document_root, insecure=True)
|
||||
|
||||
def _should_handle(self, path):
|
||||
"""
|
||||
Checks if the path should be handled. Ignores the path if:
|
||||
|
||||
* the host is provided as part of the media_url
|
||||
* the request's path isn't under the media path
|
||||
"""
|
||||
return path.startswith(self.media_url[2]) and not self.media_url[1]
|
||||
|
||||
|
||||
def run(addr, port, wsgi_handler):
|
||||
|
||||
Reference in New Issue
Block a user