Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

django and python - get user IP

  • 01-11-2018 01:42PM
    #1
    Registered Users, Registered Users 2 Posts: 10,793 ✭✭✭✭


    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