Reputation: 8467
I am using fullcalendar jquery
library to display a calendar in my django
blog application page.I have some Post
s created by a logged in user
and would like to show him a calendar view of each month with number of posts
made by him on each day or 0 if no post was made on a particular day.
I have included the javascript
and css
files given in the fullcalendar
libary and my page is showing the calendar successfully.But then how do you pass the data from the django
view to the calendar?When I checked the source of the html page,I saw that the calendar is made of html table.
Has anyone done something similar?Can you advise how to show the number of posts on each calendar day cell?
I tried to think of a solution like this,
For a particular month ,I would need to get probably a dict
where key is day(eg:1 april) and value is
len(Post.objects.filter(month='april' ,day='1'))
and pass this dict
to the response (which renders the html page above).But then how can I make the calendar take each value from this dict and display it in the appropriate cell?
If my thinking is faulty,please correct me.
I chose the fullcalendar
script because it looked nice..but I don't need any event related functions in it..So I am open to simpler soultions.If you think fullcalendar is overkill ,please suggest a simpler alternative
Upvotes: 1
Views: 2036
Reputation: 4751
You need to give fullcalendar "events" data, that could be number of posts and that will be rendered as events. According to the docs it could be in different formats, like js array. You just need to pass that data into template. Template then could look like this:
<script>
...
$('#calendar').fullCalendar({
events: {{ posts_counts|safe }}
});
...
</script>
Where posts_counts
is array of dicts in format that fullcalendar supports, look here.
View could look like this. It's rough example, just to show how it could be done:
def calendar(request):
posts_counts = []
for i in (1, 31):
posts_counts.append({'title': len(Post.objects.filter(month='april', day=i)),
'start': '2012-04-%d' % i,
'end': '2012-04-%d' % i})
return render(request, 'calendar_template.html',
{'posts_counts': simplejson.dumps(posts_counts)})
Then fullcalendar on your page should be initialized with number of posts as events. Probably you could then customize calendar look, disable events drag&drop etc.
Your solution that finds number of posts will generate a lot of SQL queries and it'd be much better to think of a better solution.
Upvotes: 4