Home > Net >  How to pass a parameter in the string in python using format
How to pass a parameter in the string in python using format

Time:06-21

There as a url link which i have in settings.py file :

KAVENEGAR_URL = "https://api.kavenegar.com/v1/{key}/verify/lookup.json?receptor={phone}&token={otp}".format(key,phone,otp)

I want to use it in service.py file inside send_otp_sms method I dont know how to pass key, phone, otp variables in it

from django.conf import settings

def send_otp_sms(key, phone, otp):
    kavenegar_url = settings.KAVENEGAR_URL.format(key, phone, otp)
    response = requests.post(kavenegar_url)
    if response.status_code != 200:
        raise APICallError
    return response

I used .format as you see in the codes but it pass error in settings.py which Unresolved reference 'key' Unresolved reference 'phone' Unresolved reference 'otp'

CodePudding user response:

The problem is this that when i use parameters in the string like this :

KAVENEGAR_URL = "https://api.kavenegar.com/v1/{key}/verify/lookup.json?receptor={phone}&token={otp}"

I have to use .format like this :

.format(key=key, phone=phone, otp=otp)

and if i use empty braces like this :

KAVENEGAR_URL = "https://api.kavenegar.com/v1/{}/verify/lookup.json?receptor={}&token={}"

I have to use .format like this :

.format(key, phone, otp)

CodePudding user response:

Tente Isso:

from django.conf import settings

def send_otp_sms(key, phone, otp):
  kavenegar_url = f"https://api.kavenegar.com/v1/{key}/verify/lookup.json?receptor={phone}&token={otp}"
  response = requests.post(kavenegar_url)
  if response.status_code != 200:
    raise APICallError
  return response
  • Related