2012-04-12 35 views
2

根据@demalexx的reply,我创建了代码来显示我的django应用程序的用户创建的帖子数量,在jquery fullcalendar.I把日历放在索引中.html并创建django视图来填充事件数据。更新jquery fullcalendar中的事件,点击上一步按钮上的Django视图

的index.html

... 
$(document).ready(function() { 
    $('#calendar').fullCalendar({ 
     events: {{posts_counts|safe}} 
}); 
... 

Django的视图

def index(request){ 
     now=datetime.datetime.now() 
     cday=now.day 
     cmonth=now.month 
     cyear=now.year 
     for i in range(1,cday+1): 
     posts_count.append({'title':str(Post.objects.filter(postauthor=request,user,posteddate__year=current_year,posteddate__month=current_month,posteddate__day=i).count()),'start':now.strftime("%Y-%m-"+str(i)),'end':now.strftime("%Y-%m-"+str(i))})} 
     return render(request, 'index.html',{'posts_counts':simplejson.dumps(posts_counts)}) 

在urls.py,我已经把URL作为

url(r'^$', 'myapp.views.index',{}, name = 'home'), 

现在,事情的工作作为expected.When我进入主页(http://127.0.0.1:8000/myapp/),当月的每一天显示当天创建的帖子数量

问题::如何在单击prev,next按钮时做同样的事情?

我想做同样的prevnext buttons.So的点击,我决定打电话给另一个Django的视图,通过月份和年份由fullCalendar('getDate') method.I编码这样回来了。

的index.html

... 
    $(document).ready(function() { 
      $('#calendar').fullCalendar({ 
       events: {{entry_count|safe}} 
      }); 

      $('.fc-button-prev').click(function(){ 

       var d=$('#calendar').fullCalendar('getDate'); 
       var month=d.getMonth()+1; 
       var year=d.getFullYear();    
        //need to call django view with these values...   
      $.ajax({ 
     url:'/myapp/monthly_posts/'+year+'/'+month, 
      type:"GET", 
      success:function(){ 
      alert("done");       
      }, 
      } 
     ); 
     }); 

      $('.fc-button-next').click(function(){ 
        //alert('next is clicked, do something'); 
         //blank for now 
       }); 

     }); 

最后,我编写了Django的观点来处理这个get请求,这是对点击urls.py的上一个按钮

def monthly_posts(request,year,month): 
    print 'monthly_posts::year=',year,' month=',month  
    posts_counts=[] 
    #find number of days in month and feed to forloop 
    days_in_month=calendar.monthrange(int(year), int(month))[1] 
    for i in range(1,days_in_month+1): 
     cdate=datetime.datetime(int(year),int(month),i) 
     posts_counts.append({ 
           'title':str(Post.objects.filter(postauthor=request.user,posteddate__year=year,posteddate__month=month,posteddate__day=i).count()), 
           'start':cdate.strftime("%Y-%m-%d"), 
           'end':cdate.strftime("%Y-%m-%d") 
           }) 
    dumped=simplejson.dumps(posts_counts) 
    print 'dumped posts=',dumped 
    return render(request, 'index.html',{'posts_counts':dumped}) 

还派出

url(r'^monthly_posts/(?P<year>\d{4})/(?P<month>\d{1,2})/$','myapp.views.monthly_posts',{}) 

这是当事情不能完全工作。当单击prev按钮时,警报框会按预期方式弹出d则执行Django的视图,在monthly_posts()打印语句获取正确的值(假设今天是april 11,我点击prev按钮,打印语句打印

monthly_posts ::年=2012个月= 3

这是正确的.. 2012年3月,因为我的JavaScript代码添加1到月份数字(这是3月3日,因为0基于javascript date.getMonth()

它也会正确输出json最后转储打印语句在视图中..我检查了在那个月发表的帖子。那里没有问题。

但是,3月份的日历视图并未显示任何事件!

当我手动输入的URL在Django视图

http://127.0.0.1:8000/myapp/monthly_posts/2012/3/ 

打印语句执行正确

month_summary::year= 2012 month= 3 

但是,月视图仍然是当月即April..I的猜测是预计.. 当我点击上一步按钮, 警报框弹出正确,

和march月视图显示正确的事件的所有天数惊喜来临。

我对此有点困惑。我需要做些什么才能使事件正确显示在单击prev按钮上?我想我缺少一些关于ajax和django工作方式的基本知识。

回答

1

使用Ajax在Django视图:

def index(request): 
    if request.is_ajax(): 
     return get_monthly_posts(request.GET['start'], request.GET['end']) 
    return TemplateResponse(request, 'index.html', {}) 

准备响应:

def get_monthly_posts(start, end): 
    #prepare data_list using start nad end variables 
    #remember that start/end variables are in timestamp 

    return HttpResponse(simplejson.dumps(data_list), mimetype='application/javascript') 

urls.py:

url(r'^index/', 'myapp.views.index', name='index') 

的index.html:

$('#calendar').fullCalendar({ 
    events: '/index/' 
}); 
相关问题