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 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

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

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


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/ file and add:

from django.shortcuts import render
from 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..',
    return HttpResponse('Message Sent Successfully..')

Step 7: Creating URLs

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

from django.urls import path
from . import views

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

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

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    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 runserver

I Hope It will help you....