dajngo发送短信
时间: 2023-11-27 16:03:38 浏览: 145
在Django中发送***ilio发送短信的示例:
首先,安装Twilio库:
```shell
pip install twilio
```
然后在Django项目中的`settings.py`文件中添加以下配置:
```python
TWILIO_ACCOUNT_SID = 'Your Twilio Account SID'
TWILIO_AUTH_TOKEN = 'Your Twilio Auth Token'
TWILIO_PHONE_NUMBER = 'Your Twilio Phone Number'
```
接下来,在你的视图中引入Twilio库,并编写发送短信的代码:
```python
from django.conf import settings
from twilio.rest import Client
def send_sms(request):
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
message = client.messages.create(
body='Hello from Django!',
from_=settings.TWILIO_PHONE_NUMBER,
to='Recipient Phone Number'
)
return HttpResponse('SMS sent!')
```
确保将`Recipient Phone Number`替换为实际接收短信的手机号码。
阅读全文