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

前端 未结 1 811
时光取名叫无心
时光取名叫无心 2021-01-06 22:42

I am trying to use django-debug-toolbar on python anywhere in a django app. It requires me to set my ip address in the settings which i\'ve done, but the toolbar is not sho

1条回答
  •  孤街浪徒
    2021-01-06 23:27

    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)
    

    0 讨论(0)
提交回复
热议问题