12

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

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

Localhost: python manage.py runserver
Not localhost: python manage.py runserver 0.0.0.0:8000

Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • The `debug` flag is set when the server is started. Wouldn't using different `settings.py` for `localhost` and other be a better solution then this? – Pratik Mandrekar Aug 19 '12 at 15:31
  • @PratikMandrekar Is there and easy way to do this from the command-line? – Tyilo Aug 19 '12 at 15:33
  • 4
    If it's just about `runserver` you could check `sys.argv`... – Bernhard Vallant Aug 19 '12 at 15:43
  • I'm confused by your examples; both `manage.py` lines will result in your app being accessible on `http://localhost:8000`. I know of no way (other than inspecting `sys.argv` as @Bernhard says) to differentiate between these two situations. – supervacuo Aug 19 '12 at 15:57
  • 1
    I'm sure you know, but just in case, runserver should not be used for anything but development and thus debug mode. Hopefully, 0.0.0.0:8000 is not for outside traffic. – Yuji 'Tomita' Tomita Aug 19 '12 at 16:11
  • I dont have my computer at hand but python's socket library has hooks for this I'm sure – Hedde van der Heide Aug 19 '12 at 16:19
  • Checking for `runserver` seems to do the job. – Tyilo Aug 19 '12 at 16:29
  • @Tyilo so you check sys.argv for 'runserver' inside your manage.py ? – SpiXel Aug 19 '12 at 16:37
  • @SpiXel inside `settings.py` actually. See my answer. – Tyilo Aug 19 '12 at 16:55
  • 4
    possible duplicate of [How can I tell whether my Django application is running on development server or not?](http://stackoverflow.com/questions/1291755/how-can-i-tell-whether-my-django-application-is-running-on-development-server-or) – zurfyx Jul 26 '13 at 08:01

4 Answers4

20

As suggested by Bernhard Vallant, you can just check for runserver in sys.argv.

You can just replace your DEBUG assignment in settings.py with this:

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

You should also import sys somewhere in settings.py.

Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • This doesn't actually answer your own question. Please update your question title (to something like "Determine if Django is running under the development server") or remove this answer. – supervacuo Aug 19 '12 at 18:03
2

This is not the best approach, but it works :)
For something better you can use django-configurations

import sys    
# Determine if in Production or Development
if (len(sys.argv) >= 2 and sys.argv[1] == 'runserver'):
    DEBUG = True 
    #...       
else:
    DEBUG = False
    #...

Or you can use it as one-liner as mentioned by little_birdie in the comments:
DEBUG = (len(sys.argv) > 1 and sys.argv[1] == 'runserver')

Slipstream
  • 13,455
  • 3
  • 59
  • 45
  • 1
    This is better than the accepted answer, because the accepted answer throws an exception if you run manage,py without arguments. However I do this as a one-liner: "DEBUG = (len(sys.argv) > 1 and sys.argv[1] == 'runserver')" – little_birdie Jan 26 '22 at 22:25
0

Could not have a permalink to this accepted and related 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 might be set on META, and have a class from a module named django.core.servers.basehttp by extreme coincidence on another server environment, but I hope this will have you covered.

PS: Please refer to How can I tell whether my Django application is running on development server or not? for more details

Community
  • 1
  • 1
GodMan
  • 2,561
  • 2
  • 24
  • 40
0
  • Simpler code.
  • you really should log it so you can know for sure
  • this works even if it's run in an environment that starts it a completely different way other than calling python. There may not be argv at position 1.
import sys

DEBUG = 'runserver' in sys.argv
print(f'DEBUG = {DEBUG}')
toddmo
  • 20,682
  • 14
  • 97
  • 107