Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

django and python - get user IP

Options
  • 01-11-2018 1:42pm
    #1
    Registered Users Posts: 10,664 ✭✭✭✭


    Hi

    Im using django and I have it running fine. Im trying to get a users IP address so in my views I have this
    [COLOR=#000080][B]from [/B][/COLOR]django.shortcuts [COLOR=#000080][B]import [/B][/COLOR]render
    [COLOR=#000080][B]from [/B][/COLOR]django.http [COLOR=#000080][B]import [/B][/COLOR]HttpResponse
    
    [COLOR=#000080][B]def [/B][/COLOR]get_client_ip(request):
        x_forwarded_for = request.META.get([COLOR=#008080][B]'HTTP_X_FORWARDED_FOR'[/B][/COLOR])
        [COLOR=#000080][B]if [/B][/COLOR]x_forwarded_for:
            ip = x_forwarded_for.split([COLOR=#008080][B]','[/B][/COLOR])[-[COLOR=#0000ff]1[/COLOR]].strip()
        [COLOR=#000080][B]else[/B][/COLOR]:
            ip = request.META.get([COLOR=#008080][B]'REMOTE_ADDR'[/B][/COLOR])
        [COLOR=#000080][B]return [/B][/COLOR]ip
    
    

    I get the error
    Exception Value: 'str' object has no attribute 'get'
    

    iver been googling it like mad and I can't make head nor tail or what I find. Does anyone have any links to a tutorial running through how to do this?


Comments

Advertisement