How do I get django-debug-toolbar to only display on my ip address hosted on python anywhere?

爷,独闯天下 提交于 2019-12-01 11:24:23

Python anywhere sets a custom definition in the headers called

HTTP_X_REAL_IP

This is the IP address from which pythonanywhere receives the request, and that seems to work best for getting the actual client IP.

You could also use HTTP_X_FORWARDED_FOR but in theory that could contain a set of different IP addresses if the incoming request went through some kind of proxy prior to getting to pythonAnywhere.

To do this, there are two options.

first, you can add this to your settings.py

def custom_show_toolbar(request.META.get('HTTP_X_REAL_IP', None) in INTERNAL_IPS):
    return True 
# Show toolbar, if the IP returned from HTTP_X_REAL_IP IS listed as INTERNAL_IPS in settings
    if request.is_ajax():
        return False
# Show toolbar, if the request is not ajax
    return bool(settings.DEBUG)
# show toolbar if debug is true

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
}

Or you could modify the file middleware.py inside of the django-debug-toolbar folder, and changing the following code:

def show_toolbar(request):
    """
    Default function to determine whether to show the toolbar on a given page.
    """
    if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS:
        return False

    if request.is_ajax():
        return False

    return bool(settings.DEBUG)

To:

def show_toolbar(request):
    """
    Default function to determine whether to show the toolbar on a given page.
    """
    if request.META.get('HTTP_X_REAL_IP', None) not in settings.INTERNAL_IPS:
        return False

    if request.is_ajax():
        return False

    return bool(settings.DEBUG)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!