from datetime import datetime, timedelta import jwt def generate_jwt_token(user): payload = { 'user_id': user.id, 'exp': datetime.utcnow() + timedelta(days=14), # 令牌有效期为两周 'iat': datetime.utcnow() } token = jwt.encode(payload, 'django-insecure-#ru&o+c%h83_gp_wo=z#wub(#4vn4)xz*c2!24i1ft!+v^_unm', algorithm='HS256') # 'your_secret_key' 替换为你自己的密钥 return token