url_for(' .outbound',_ external = True)是什么意思?

时间:2018-01-18 08:06:44

标签: python flask

我有一个像这样的python Twilio代码(twilio中的Click to Call方法):

from flask import Flask
from flask import jsonify
#from flask import render_template
#from flask import request
from flask import url_for

from twilio.twiml.voice_response import VoiceResponse
from twilio.rest import Client

app = Flask(__name__)

# Voice Request URL
@app.route('/call')
def call():
    # Get phone number we need to call
    phone_number = request.form.get('phoneNumber', None)

    try:
        twilio_client = Client(app.config['TWILIO_ACCOUNT_SID'],
                               app.config['TWILIO_AUTH_TOKEN'])
    except Exception as e:
        msg = 'Missing configuration variable: {0}'.format(e)
        return jsonify({'error': msg})

    try:
        twilio_client.calls.create(from_=app.config['TWILIO_CALLER_ID'],
                                   to=phone_number,
                                   url=url_for('.outbound', _external=True))
    except Exception as e:
        app.logger.error(e)
        return jsonify({'error': str(e)})

    return jsonify({'message': 'Call incoming!'})


@app.route('/outbound', methods=['POST'])
def outbound():
    response = VoiceResponse()

    response.say("Thank you for contacting our sales department. If this "
                 "click to call application was in production, we would "
                 "dial out to your sales team with the Dial verb.",
                 voice='alice')

    response.number("+16518675309")

    return str(response)

if __name__ == '__main__':
    app.run()

当我尝试从浏览器运行此命令时,请致电:http://localhost:5000/call
我收到错误:无法创建记录:Url不是有效的网址:

如何调用网址中的出站功能并开始两人之间的对话。

1 个答案:

答案 0 :(得分:1)

而不是url_for('.outbound', _external=True),您应该使用url_for('outbound')docs linked by stamaimer说:

  

如果蓝图处于活动状态,您可以通过在本地端点前加一个点(。)来快捷方式对同一蓝图的引用。

开头不需要点。检查url building is handled in flask

的方式