tags:

views:

166

answers:

3

Hello!

I want to enable debug (DEBUG = True) For my Django project only if it runs on localhost. How can I get user IP address inside settings.py? I would like something like this to work:

#Debugging only on localhost
if user_ip = '127.0.0.1':
    DEBUG = True
else:
    DEBUG = False 

How do I put user IP address in user_ip variable inside settings.py file?

+3  A: 

Maybe it is enough for you to specify some INTERNAL_IPS: http://docs.djangoproject.com/en/dev/ref/settings/#setting-INTERNAL_IPS

zefciu
A: 

use this.

import socket

print socket.gethostbyname_ex(socket.gethostname())[2]

edit: ah, i had misunderstood the topic.

emre yılmaz
A: 

Try this in you settings.py

class LazyDebugSetting(object):
    def __init__(self):
        self.value = None
    def __nonzero__(self):
        if not self.value:
           # as emre yilmaz say
           user_ip = socket.gethostbyname_ex(socket.gethostname())[2]
           self.value = user_ip == '127.0.0.1'
        return self.value 
    __len__ = __nonzero__

DEBUG = LazyDebugSetting()

But better use the INTERNAL_IPS

Or use environment variables

DEBUG = os.environ.get('DEVELOP_MODE', False)
estin