Determine if Django is running under the development server

Is there a way to determine if Django is running on the local host and sets the DEBUG variable in settings.py accordingly.

So, if I run the server locally, it will set DEBUG to True and otherwise set it to False .

Local address: python manage.py runserver
Not localhost: python manage.py runserver 0.0.0.0:8000

+8
python django
source share
2 answers

As suggested by Bernhard Vallant, you can simply check the runserver at sys.argv .

You can simply replace the DEBUG assignment in settings.py following:

 DEBUG = (sys.argv[1] == 'runserver') 

You should also import sys somewhere in settings.py .

+10
source share

There can be no permanent reference to this accepted and corresponding answer to your question. So just pasting it: -

 server = request.META.get('wsgi.file_wrapper', None) if server is not None and server.__module__ == 'django.core.servers.basehttp': print 'inside dev' 

Of course, wsgi.file_wrapper can be installed on META and have a class from a module named django.core.servers.basehttp using extreme matching in a different server environment, but I hope this affects you.

PS: see How do I know if a Django application is running on a development server? More details

0
source share

All Articles