- June 2, 2010
- 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
- Image compression before saving the new model / work with JPG, PNG by Schleidens 2 weeks ago
- Help text hyperlinks by sa2812 1 month, 1 week ago
- Stuff by NixonDash 3 months, 2 weeks ago
- Add custom fields to the built-in Group model by jmoppel 5 months, 2 weeks ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 9 months ago
Also works in you put the file with django's 'built-in' commands :-
Please login first before commenting.