在django应用程序中使用jquery fullcalendar [英] using jquery fullcalendar in django app

查看:992
本文介绍了在django应用程序中使用jquery fullcalendar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fullcalendar jquery 库来显示我的 django 博客应用程序页面中的日历。我有一些 Post 由登录 user ,并希望向他显示每个月的日历视图,每个日期有个帖子,如果没有帖子,则为0

I am using fullcalendar jquery library to display a calendar in my django blog application page.I have some Posts 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.

我已包含 javascript css 文件在 fullcalendar libary和我的页面显示日历成功。然后如何传递数据从 django 查看日历?当我检查html页面的源,我看到日历是由html表。

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?

我试着想到一个像这样的解决方案,
For a特定的月份,我需要得到一个 dict 其中键是天(例如:1 april),值是

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'))

并将此 dict 传递给响应(呈现上面的html页面)。然后如何使日历

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.

我选择了 fullcalendar 脚本,因为它看起来不错,但我不需要任何事件相关的功能。它是开放的更简单的soultions.If

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

推荐答案

您需要提供完整的事件数据,可能是数字帖子,并将作为事件呈现。根据docs它可以是不同的格式,如js数组。你只需要将数据传递到模板。模板可能如下所示:

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>

其中 posts_counts 该fullcalendar支持,请此处查看。

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.

您的解决方案找到的帖子会生成大量的SQL查询,更好地想一个更好的解决方案。

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.

这篇关于在django应用程序中使用jquery fullcalendar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆