![](/img/trans.png)
[英]Twilio SMS 2FA in Python/Flask, server not rendering route
[英]Flask SMS with twilio error
from flask import *
from twilio import twiml
from twilio.rest import TwilioRestClient
from flask import render_template
import os
#Pull in configuration from system environment variables
TWILIO_ACCOUNT_SID = os.environ.get('Axxxxxx')
TWILIO_AUTH_TOKEN = os.environ.get('Sxxxxxxxxx')
TWILIO_NUMBER = os.environ.get('xxxxxxx')
# create an authenticated client that can make requests to Twilio for your
# account.
#client = TwilioRestClient(account='Axxxxx', token'sxxxxxxxx')
#create a flask web app
app = Flask(__name__)
client = TwilioRestClient(account='Axxxxx', token='Sxxxxx')
@app.route('/')
def homepage():
return render_template('index.html')
#Handling a post request to send text messages.
@app.route('/message', methods=['POST', 'GET'])
def message():
# Send a text message to the number provided
if request.method == 'POST':
message = client.sms.messages.create(to=request.form['Phone_number'],
from_=TWILIO_NUMBER,
body=request.form['body'])
return render_template('message.html')
if __name__ == '__main__':
# Note that in production, you would want to disable debugging
app.run(debug=True)
我正在用燒瓶。 當我輸入數字和短信時,它給我這個錯誤
Method Not Allowed
The method is not allowed for the requested URL.
您正在發布到錯誤的端點。 您的表單應如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Send an SMS</title>
</head>
<body>
<form action="/message" method="POST">
Cell phone number: <input name="phone_number" type="text" />
Text in here: <input name="body" type="text" />
<button type="submit">Send Text</button>
</form>
</script>
</body>
</html>
(以上action
已從/
更改為/message
。)
注意:如果這是通過flask.render_template
運行的模板,則應更改
<form action="/message" method="POST">
至
<form action="{{ url_for('message') }}" method="POST">
這是在flask中使用URL的更可持續的方法,如果您需要更改值,它將減少您的開銷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.