Login

Quiet runserver

Author:
danielroseman
Posted:
June 2, 2010
Language:
Python
Version:
1.2
Tags:
runserver management-command
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

  1. pyserver -- runserver alias by presclark 6 years, 9 months ago
  2. Serve admin-media from urls.py by wolever 3 years, 5 months ago
  3. backupdb command by msaelices 6 years, 8 months ago
  4. Continuous Integration command by berto 5 years, 4 months ago
  5. reset_db management command by dnordberg 6 years, 8 months ago

Comments

d-rave (on June 15, 2010):

Also works in you put the file with django's 'built-in' commands :-

django/core/management/commands/

#

Please login first before commenting.