最近,實際一些簡單統計時,要到庫里去檢索數據出來用HIGHCHARTS畫圖,
作一個簡單的回照。。
?
DJANGO用TEMPLATEVIEW來作。專業,正規:)
class SAView(TemplateView):template_name = 'version/sa_site.html'paginate_by = 10def get_context_data(self, **kwargs):context = super(SAView, self).get_context_data(**kwargs)site_dict = {}appcount = A.objects.annotate(num_app=Count('dv'))for app in appcount:if self.request.GET.has_key('date_start') and self.request.GET.has_key('date_end') :date_start = self.request.GET['date_start']date_end = self.request.GET['date_end']context['days'] = date_start+'至'+date_endapp_qryset = app.deployversion_set.filter(add_date__range=(date_start, date_end))else:context['days'] = '所有時間'app_qryset = app.deployversion_set.all()if app.site_set.all() and app_qryset.count():site_key = str(app.site_set.all()[0].name)if site_dict.has_key(site_key):site_dict[site_key] += app_qryset.count()else:site_dict[site_key] = app_qryset.count()categories = site_dict.keys()data = site_dict.values()context['now'] = timezone.now()context['current_page'] = "list-sa-site"context['form'] = SASiteFormcontext['categories'] = categoriescontext['data'] = datareturn context
前端JAVASCRIPT的小東東,找偉哥作了那個最近一周和一月的東東,很好:)感謝:
Date.prototype.Format = function(fmt) {var o = {"M+" : this.getMonth()+1,"d+" : this.getDate(),"h+" : this.getHours(),"m+" : this.getMinutes(),"s+" : this.getSeconds(),"q+" : Math.floor((this.getMonth()+3)/3),"S" : this.getMilliseconds()};if(/(y+)/.test(fmt)){fmt = fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));}for(var k in o){if(new RegExp("("+ k +")").test(fmt)){fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));}}return fmt; }$(".search_btn").click(function(){var date_start = $("input[name='date_start']").val() || "demo";var date_end = $("input[name='date_end']").val() || "demo";var date_today = new Date().Format("yyyy-MM-dd");console.log(date_today);if (date_start > date_end) {alert('開始時間大于結束時間,請重新選擇');return;};if ((date_start >= date_today) || (date_end >= date_today)) {alert('開始時間和結束時間不能超過當前時間');return;};console.log(date_start, date_end);var url = "/sa/site/?date_start=" + date_start + "&date_end=" + date_endconsole.log(url)location.href = url}); $(".search_btn_week").click(function(){var current = new Date();var utcDate = current.setDate(current.getDate()-7);var date_start = new Date(utcDate).Format("yyyy-MM-dd");var date_end = new Date().Format("yyyy-MM-dd");console.log(date_start, date_end);var url = "/sa/site/?date_start=" + date_start + "&date_end=" + date_endconsole.log(url)location.href = url}); $(".search_btn_month").click(function(){var current = new Date();var utcDate = current.setDate(current.getDate()-30);var date_start = new Date(utcDate).Format("yyyy-MM-dd");var date_end = new Date().Format("yyyy-MM-dd");console.log(date_start, date_end);var url = "/sa/site/?date_start=" + date_start + "&date_end=" + date_endconsole.log(url)location.href = url});
Form結合了UIKIT的時間PICKER樣式:
class SASiteForm(forms.Form):date_start = forms.CharField(max_length=100,label=u"開始日期",widget=forms.TextInput(attrs={'class': 'uk-width-1-6','data-uk-datepicker': "{format:'YYYY-MM-DD'}",}),)date_end = forms.CharField(max_length=100,label=u"結束日期",widget=forms.TextInput(attrs={'class': 'uk-width-1-6','data-uk-datepicker': "{format:'YYYY-MM-DD'}",}),)
AND THEN。。。
?