获取一个引导日历组件来使用Django [英] Getting a bootstrap-calendar component to work with Django

查看:260
本文介绍了获取一个引导日历组件来使用Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个问题是否太具体,但是我会给它一个镜头:

I don't know if this question is too specific but I'll give it a shot anyway:

我发现一个非常好的自举日历是非常有用的。该组件位于此处: http://bootstrap-calendar.azurewebsites.net/

I found a very nice bootstrap calendar that is very useful. The component is found here: http://bootstrap-calendar.azurewebsites.net/

我正在尝试使用事件填充该日历。我正在使用django。我做了一个返回JSON格式的元素的函数。我正在弥补现在的日期,只是现在更容易查看日历:

I am trying to populate that calendar with events. I am using django. I made a function to return the elements on JSON format. I am making up the dates for now, just so its easier to view on the calendar for now:

def view_list_json(request):
i = 0
json_string = '{"sucess": 1, result: ['
for run in Model.objects.all():
    start = datetime.datetime.now() - datetime.timedelta(days = i)
    end = datetime.datetime.now() - datetime.timedelta(days = i) + datetime.timedelta(minutes = 40)

    start_str = str(int(time.mktime(start.timetuple())))
    end_str = str(int(time.mktime(end.timetuple())))

    json_string += '{ "id": "' + str(run.id) + '"'
    json_string += ', "title": "Foo"'
    json_string += ', "url":"#"'
    json_string += ', "class": "event-success"'        
    json_string += ', "start":"' + start_str + '"'
    json_string += ', "end":"' + end_str + '"},'
    i += 1
    json_string = json_string[:-1]
    json_string += ']}'

    return HttpResponse(json_string, content_type="application/json")

JSON格式我正在尝试创建:

JSON format I am trying to create:

https://github.com/Serhioromano/bootstrap-calendar/blob/master/events.json.php

该组件似乎正在阅读此功能正确。我在Firebug控制台上没有任何错误。

The component seems to be reading this function correctly. I don't get any errors in this sense on Firebug console...

...然而,数据似乎没有加载。

... Yet the data doesn't seem to load.

有什么想法可以解决问题?

Any ideas on how I can approach the problem?

推荐答案

这个JSON根本无效。有一些问题,比如每个元素之后的额外的大括号,元素之间的逗号,在下一个元素的开头缺少打开的方括号。

This JSON is not valid at all. There are quite a few problems, like extra close braces after each element, missing commas between elements, and missing open square brackets at the beginning of the next element.

但是真的您不应该尝试构建一个这样的JSON字符串 - 它太容易出错。即使你对它们进行了严格的编码,你仍然应该使用标准的Python列表和dict来构建它,然后使用内置的 json 库进行序列化。

But really you shouldn't be trying to build up a JSON string like that - it's too prone to errors. Even if you're hard-coding them, you should still build it up using standard Python lists and dicts, then serialize using the built-in json library.

这篇关于获取一个引导日历组件来使用Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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