- Author:
- danielroseman
- Posted:
- June 2, 2010
- Language:
- Python
- Version:
- 1.2
- Score:
- 2 (after 4 ratings)
It's quite common to use Django's static.serve
functionality to serve media assets via the built-in development server. However, I always find that this is far too noisy - every asset produces a line of output on the console, and any debug messages I want to put there are hard to see.
This management command monkey-patches the built-in runserver
command so that it only generates a line of output for actual Django views - anything else is served as usual, but is not logged to the console. In fact the original version was already doing this for admin media, but not for your own media - I've just extended the logic.
Put this in the management/commands directory of an installed app, saving it as (for example) runserver_quiet
, then just do ./manage.py runserver_quiet
to run - it will accept all the same arguments as the built-in version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from django.conf import settings
from django.core.servers import basehttp
from django.core.management.commands.runserver import Command as BaseCommand
class QuietWSGIRequestHandler(basehttp.WSGIRequestHandler):
def log_message(self, format, *args):
# Don't bother logging requests for paths under MEDIA_URL.
if self.path.startswith(settings.MEDIA_URL):
return
# can't use super as base is old-style class, so call method explicitly
return basehttp.WSGIRequestHandler.log_message(self, format, *args)
def run(addr, port, wsgi_handler):
server_address = (addr, port)
httpd = basehttp.WSGIServer(server_address, QuietWSGIRequestHandler)
httpd.set_app(wsgi_handler)
httpd.serve_forever()
class Command(BaseCommand):
def handle(self, addrport='', *args, **options):
# monkeypatch Django to use our quiet server
basehttp.run = run
return super(Command, self).handle(addrport, *args, **options)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Also works in you put the file with django's 'built-in' commands :-
django/core/management/commands/
#
Please login first before commenting.