You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django中获取客户端IP并判断注册日期是否为用户所在国节假日

实现步骤

1. 获取用户真实IP地址

Django中直接调用request.META.get('REMOTE_ADDR')只能拿到直接连接服务器的IP,若用户通过反向代理(如Nginx、Cloudflare)访问,需要处理转发头来获取真实IP:

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        # 多个代理时取第一个非内网IP
        ip = x_forwarded_for.split(',')[0].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

注意要在服务器配置中信任代理IP,避免头被伪造,比如在Django设置里配置ALLOWED_HOSTS,若用HTTPS还需设置SECURE_PROXY_SSL_HEADER。

2. 通过IP定位用户所在国家

使用django-geoip2结合本地IP数据库(无需调用外部API):

  • 安装依赖:pip install django-geoip2
  • 下载MaxMind免费的GeoLite2-Country数据库,解压后放在项目的geoip目录下
  • 在Django配置文件中添加:
import os
from django.conf import settings

GEOIP_PATH = os.path.join(settings.BASE_DIR, 'geoip')
  • 编写获取国家代码的函数:
from django_geoip2.utils import get_geoip2

def get_user_country(ip):
    geoip = get_geoip2()
    try:
        response = geoip.country(ip)
        return response.country.iso_code  # 返回ISO标准国家代码,如'CN'、'US'
    except:
        return None  # 处理IP无法定位的异常情况

3. 判断注册日期是否为该国节假日

用python-holidays库查询各国节假日数据:

  • 安装依赖:pip install holidays
  • 编写判断函数:
import holidays

def is_holiday(country_code, date):
    if not country_code:
        return False
    try:
        country_holidays = holidays.CountryHoliday(country_code)
        return date in country_holidays
    except KeyError:
        return False  # 处理库不支持的国家代码

整合逻辑

在用户注册视图中串联三个步骤:

from django.contrib.auth.models import User
from django.utils import timezone

def register(request):
    if request.method == 'POST':
        # 处理用户注册表单逻辑
        user = User.objects.create_user(
            username=request.POST['username'],
            password=request.POST['password'],
            email=request.POST['email']
        )
        user.save()
        
        # 获取IP并定位国家
        ip = get_client_ip(request)
        country_code = get_user_country(ip)
        
        # 判断注册日期是否为节假日
        register_date = timezone.now().date()
        reg_on_holiday = is_holiday(country_code, register_date)
        
        # 可将结果保存到用户扩展模型或用于后续业务逻辑
        # 示例:假设用户有Profile扩展模型
        user.profile.country_code = country_code
        user.profile.is_reg_on_holiday = reg_on_holiday
        user.profile.save()
        
        # 后续响应逻辑...
    # GET请求处理逻辑...

内容的提问来源于stack exchange,提问作者Krystian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 03:57:14