通过Angular将表单数据发布到Flask Web应用程序并返回新呈现的模板

时间:2016-02-19 15:52:26

标签: angularjs flask

我有以下AngularJS,HTML和Flask代码。我可以从下拉列表中进行选择,并将值提交给Web服务器。 Web服务器正在打印选择,但我想知道如何使用提供的 serial_no 值返回新呈现的模板 report_filter.html 。我在页面的其他地方显示此值。邮件请求已发送,但新页面未呈现。这个意图应该如何实现?谢谢!

HTML

<form ng-submit="printReport(selectedDevice)" name="deviceSelectionForm" novalidate>
    <legend>Select Device</legend>
    <label for="device_selection">serial number</label>
    <select ng-model="selectedDevice" class="form-control" id="device_selection" ng-options="DeviceSerialNo.device_id for DeviceSerialNo in DeviceSerialNos"></select>
    <button class="btn btn-primary" type="submit" ng-disabled="deviceSelectionForm.$invalid">Submit</button>
</form>

AngularJS

var dlrApp = angular.module('dlrApp', []);

dlrApp.controller('MainCtrl',
    function ($scope, $http) {
        $scope.DeviceSerialNos = [
            {% for device_id in device_id_rows %}
                {device_id: "{{device_id}}"},
            {% endfor %}
        ];

        $scope.selectedDevice = {};

        $scope.printReport = function(filterParams) {
            $http.post("dataset", JSON.stringify(filterParams), null)
            .success(function (data, status, headers, filterConfig)
            {
                $scope['id'] = data;
            })
            .error(function (data, status, headers, filterConfig)
            {
                $scope['id'] = "SUBMIT ERROR";
            });
        }
    }
);

烧瓶

@app.route('/', methods=['GET', 'POST'])
@app.route('/dataset', methods=['GET', 'POST'])
def report_filter():
    device_id_rows = [800, 801, 802, 803, 804]
    json_data = request.get_json()

    if json_data is not None and "device_id" in json_data:
        serial_no = json_data["device_id"].__str__()
    else:
        serial_no = "unknown"

    print serial_no
    return render_template('report_filter.html', device_id_rows=device_id_rows, serial_no=serial_no)

0 个答案:

没有答案