- June 2, 2010
- runserver management-command
- 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
- pyserver -- runserver alias by presclark 7 years, 8 months ago
- Serve admin-media from urls.py by wolever 4 years, 5 months ago
- backupdb command by msaelices 7 years, 7 months ago
- Continuous Integration command by berto 6 years, 3 months ago
- reset_db management command by dnordberg 7 years, 7 months ago