Reputation: 45922
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?
Upvotes: 2
Views: 2906
Reputation: 2046
Maybe it is enough for you to specify some INTERNAL_IPS: https://docs.djangoproject.com/en/dev/ref/settings/#internal-ips
Upvotes: 5
Reputation: 3121
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)
Upvotes: 0
Reputation: 31
use this.
import socket
print socket.gethostbyname_ex(socket.gethostname())[2]
edit: ah, i had misunderstood the topic.
Upvotes: 3