Django - distinguish development server (manage.py runserver) from the regular one (eg. Apache)

3.1k views Asked by At

I need a way to distinguish development server in Django (run eg. by ./manage.py runserver 0.0.0.0:8000) from the one being run on Apache. In case of Apache I use WSGI daemon mode, but I would like to find some more reliable solution for detecting execution on Django's development server.

What I currently use is similar to:

wsgi_wrapper = request.META.get('wsgi.file_wrapper')
wsgi_wrapper_path = wsgi_wrapper.__module__ if wsgi_wrapper else None

which seemingly gives wsgi_wrapper_path storing:

  • "wsgiref.util" string when run on Django's built-in development server,
  • None when run on Apache / WSGI in daemon mode,

The problem here is I am not sure if I can rely on that check (eg. if the production/staging/development server or my localhost configuration changes). I was not able to find any documentation regarding that.

I need that check primarily because of one issue with Django's development server: it sets request's CONTENT_TYPE (request.META['CONTENT_TYPE']) to "text/plain" even if the HTTP request itself had no content type set (eg. it was a GET request).

Any ideas regarding Django's development server detection (or solving issue with incorrect request content type on it) will be appreciated. Thanks.

Ps. I am basically asking this question: How can I tell whether my Django application is running on development server or not? for Django 1.4, trying to determine the solution that will be reliable enough in case configuration changes.

1

There are 1 answers

4
Tisho On BEST ANSWER

As a principle - when using a Production and Dev servers, they should have different configs - it is most common that they will use different databases, different mail settings, etc. So the best place to tell the kind of the server is the configuration. I can point to two most-used approaches:

  • Config file

then in settings.py just add:

server = config.get("SERVER_TYPE", "DEV")

and have SERVER_TYPE only in the config file on PRODUCTION

  • Environment

Set SERVER_TYPE as environment variable when running the web server. I.e. for Supervisord, running the webserver:

environment=SERVER_TYPE=PROD

or for Apache:

SetEnv SERVER_TYPE PROD

This way you will be sure that as long as you don't mix the config files, you will always get the real server type, no matter how you start the server. At least that why config files are used for.