2014-08-29 50 views
0

我是一個初學者,所以我甚至不知道最好的方法來做到這一點或什麼叫我想要做什麼,但我正在製作一個活動發佈應用程序,其中包含月份和年份的標題,如this jsfiddle

var calendar = angular.module('calendar', []); 
calendar.controller('month_and_year', ['$scope', function ($scope) { 
$scope.month = {{current_month}}; 
$scope.year = {{current_year}}; 
$scope.month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 
$scope.add_one = function() { 
    if ($scope.month == 12) { 
     $scope.year++; 
     $scope.month = 1; 
    } else { 
     $scope.month++ 
    } 
}; 
$scope.sub_one = function() { 
    if ($scope.month == 1) { 
     $scope.year--; 
     $scope.month = 12; 
    } else { 
     $scope.month-- 
    } 
}; 
}]); 

我的Python的重要組成部分,是這樣的:

import datetime 
now = datetime.datetime.now() 
current_month=now.month 
current_year=now.year 

def get_days_for_dates(year): 
    dates = calendar.Calendar().yeardayscalendar(year) 
    days_of_week= ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] 
    dates_list=[] 
    days_list=[] 
    for quarter in dates: 
     for month in quarter: 
      for week in month: 
       for i in range(len(week)): 
        dates_list.append(week[i]) 
        days_list.append(days_of_week[i]) 
    return days_list, dates_list 

calendar_tuple = get_days_for_dates(current_year) 

所以我的問題是,我想在我的Python使用$ scope.year其中CURRENT_YEAR是使用日曆模塊,並拿出每個日期的星期幾在哪一天。將這些信息提供給後端的最佳方法是什麼?

回答

4

您將要使用AJAX請求將數據發送回服務器。這有兩塊:

第一個是在後端創建一個端點,它允許您發送請求並檢索數據有效載荷。

@app.route('/day-lookup', method=['GET']) 
def day_lookup(): 
    year = request.args.get('year', None) 
    # error handle here 

    calendar_tuple = get_days_for_dates(year) 
    # do something with it, return the list, etc.  

第二部分是使用Angular發送數據並處理響應。

var calendar = angular.module('calendar', []); 
calendar.controller('month_and_year', ['$scope', '$http', function ($scope, $http) { 
$scope.month = {{current_month}}; 
$scope.year = {{current_year}}; 
$scope.month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 
$scope.add_one = function() { 
    if ($scope.month == 12) { 
     $scope.year++; 
     $scope.month = 1; 
    } else { 
     $scope.month++ 
    } 
}; 
$scope.sub_one = function() { 
    if ($scope.month == 1) { 
     $scope.year--; 
     $scope.month = 12; 
    } else { 
     $scope.month-- 
    } 
}; 
$scope.send_year = function() { 
    // Add the year as a parameter to GET request to your URL 
    var url = "http://foo.com/?year=" + $scope.year; 
    // Send info to the server, then handle the result 
    $http.get(url).then(function (result) { 
     // Do something with the result 
    }); 
}; 
}]); 
相關問題