How to Send SMS using Twilio in Django?

Published On: 29/11/2022 | Category: Django


Hi Dev,

This is a short guide on how to send sms using twilio in django. if you want to see example of django send sms using twilio example then you are a right place. let’s discuss about twilio django send sms. I explained simply step by step how to send message to mobile using django. follow bellow step for how to send text message using twilio in django.

Twillio is a cloud communications platform that lets you integrate voice and SMS capabilities into your app. It is a paid platform, but for the purposes of this tutorial, we will use the trial account.

Here i explained simply step by step example of how to send sms using twilio in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python manage.py startapp core

Step 3 : Install a twilio library

First of all let’s install django and twilio which are the necessary Python libraries.

pip install twilio

Step 4: Update setting.py

In this step we require to do two things in our settings.py file, One is our installed app name Add the below lines to your settings.py file:

Next, you need to add it in the settings.py file as follows:

example/settings.py
....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

Step 5: Twilio Configuration

In this step, to use Twilio, we need a trial number, account sid, and auth token.



Click on the "Get a trial number" button to get a trial number.



Our account-specific configuration credentials can be found on the Dashboard page of the account console, as shown below:

Step 6: Creating the Views

In this step, we need to create the views for performing the fetch record to the database.Open the core/views.py file and add:

core/views.py
from django.shortcuts import render
from twilio.rest import Client
from django.http import HttpResponse

def home(request):
    account_sid = 'YOUR_ACCOUNT_SID'
    auth_token = 'YOUR_AUTH_TOKEN'
    client = Client(account_sid, auth_token)

    message = client.messages.create(
                                body='Hi there..',
                                from_='YOUR_TRIAL_NUMBER',
                                to='VERIFIED_NUMBER' 
                            )
    
    return HttpResponse('Message Sent Successfully..')

Step 7: Creating URLs

In this section, we need a urls.py file within the apis app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and paste below code.

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]

Next, we will require the modify the urls.py your root preoject folder lets update the file.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls')),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

I Hope It will help you....