关于DJANGO和JAVASCRIPT的时间

最近,实际一些简单统计时,要到库里去检索数据出来用HIGHCHARTS画图,

作一个简单的回照。。

 

DJANGO用TEMPLATEVIEW来作。专业,正规:)

class SAView(TemplateView):
    template_name = 'version/sa_site.html'
    paginate_by = 10

    def 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_end
                app_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'] = SASiteForm
        context['categories'] = categories
        context['data'] = data
        
        return 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_end
        console.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_end
        console.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_end
        console.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。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值