Send SMS in Django
pip install twilio
edit the settings.py
file.
INSTALLED_APPS = [
'django_twilio',
]
views.py
from django.shortcuts import render
from twilio.rest import Client
from django.conf import settings
from twilio.base.exceptions import TwilioRestException
def index(request):
to = '+8801953664967'
try:
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
# Create and send the SMS
message = client.messages.create(
body="jkj",
from_=settings.TWILIO_PHONE_NUMBER,
to=to
)
return render(request, 'index.html', {'message_sid': message.sid})
except TwilioRestException as e:
# Handle Twilio exceptions (e.g., invalid phone number, insufficient funds, etc.)
return render(request, 'index.html', {'error_message': str(e)})
#html to show error or message
{{message_sid}}
{{error_message}}
Last updated