title: updating events in jquery fullcalendar with django view on clicking prev button
body: <p>Based on <a href="https://stackoverflow.com/questions/10069997/using-jquery-fullcalendar-in-django-app/10089534#10089534">reply</a> by @demalexx  ,I created the code to show number of posts created by a user of my django app, in jquery fullcalendar.I put the calendar in index.html and created the django view to populate events data.</p>
  
  <p>index.html</p>
  
  <pre><code>...
  $(document).ready(function() {
     $('#calendar').fullCalendar({
         events: {{posts_counts|safe}}
  });
  ...
  </code></pre>
  
  <p>django view</p>
  
  <pre><code>    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)})
  </code></pre>
  
  <p>In urls.py ,I have put the url as </p>
  
  <pre><code>url(r'^$', 'myapp.views.index',{}, name = 'home'),
  </code></pre>
  
  <p>Now,things work as expected.When I go to the homepage (<code>http://127.0.0.1:8000/myapp/</code>),the each day of the current month shows the number of posts created on that day</p>
  
  <p><strong>Question::How to do the same on clicking the prev ,next buttons?</strong></p>
  
  <p>I wanted to do the same on click of <code>prev</code> and <code>next</code> buttons.So,I decided to call another django view, passing the month and year as returned by <code>fullCalendar('getDate')</code> method.I coded like this.</p>
  
  <p>index.html</p>
  
  <pre><code>...
      $(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
                  });
  
          });
  </code></pre>
  
  <p>Finally,I coded the django view to handle this get request-which is sent on clicking the prev button</p>
  
  <pre><code>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})
  </code></pre>
  
  <p>Also in urls.py</p>
  
  <pre><code>url(r'^monthly_posts/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{1,2})/$','myapp.views.monthly_posts',{})
  </code></pre>
  
  <p>Here is when things don't work fully..When,prev button is clicked,the alert box pops up as expected and then the django view is executed,the print statement in <code>monthly_posts()</code> gets the correct values (suppose today is <code>april 11</code>, and I click on <code>prev</code> button ,the print statement prints </p>
  
  <p>monthly_posts::year= 2012  month= 3</p>
  
  <p>which is correct ..ie 2012 march ,since my javascript code adds a 1 to the month number (which is otherwise 2 for march-because of 0 based <code>javascript date.getMonth()</code> )</p>
  
  <p>and it also outputs the json dump correctly at the last print statement in the view..I checked for the Posts made in that month.No problem there.</p>
  
  <p>However,the calendar view for month of March doesnot show any events! </p>
  
  <p>When I manually entered the url </p>
  
  <pre><code>http://127.0.0.1:8000/myapp/monthly_posts/2012/3/
  </code></pre>
  
  <p>print statements in django view executes correctly </p>
  
  <pre><code>month_summary::year= 2012  month= 3
  </code></pre>
  
  <p>but,the month view is still that of current month ie April..I guess that is to be expected..
  Surprise comes when I click the prev button,
  the alert box pops up correctly ,   </p>
  
  <p>and the month view for march shows all days with the events correctly..!</p>
  
  <p>I am a bit confused by this..What do I have to do to get the events displayed properly on clicking prev button?I guess I am missing some fundamental thing here about the way ajax and django work.</p>