django开发网站,当访问网站的时候,网站如何获取到用户的ip地址?

客户端:192.168.50.79

服务器端:192.168.50.69

view.py

def visitor_ip_address(request):

    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip
def index(request):

    visitorIp = visitor_ip_address(request)
    context = {
        'visitorIp':visitorIp,

    }

    return render(request, 'scores/index.html', context)

templates/index.html

<footer id="main-footer" class="py-4 bg-dark text-white text-center">
    Copyright ©
     {{visitorIp}}
</footer>

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!



0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注