Django Jquery 获取 URL Conf

发布于 2024-12-05 23:34:40 字数 1955 浏览 2 评论 0原文

好的,所以我尝试

def user_timetable(request, userid):
    user = get_object_or_404(TwobooksUser,id = userid)
    timeSlots = TimeSlot.objects.filter(user = request.user)
    rawtimeslots = []
    for timeSlot in timeSlots:
        newSlot = {
            'userid': timeSlot.user.id,
            'startTime': str(timeSlot.startTime),
            'endTime': str(timeSlot.endTime),
        }
        rawtimeslots.append(newSlot)
    return HttpResponse(simplejson.dumps(rawtimeslots))

通过 javascript 调用该函数,其中

{% include 'elements/header.html' %}

    <script type='text/javascript'>

        $(document).ready(function() {

            $.get('/books/personal{{ user.id }}/timetable/', {}, function(data) {

                data = JSON.parse(data);
                var events = new Array();
                for (var i in data) {
                    events.push({
                        id: data[i].id,
                        title: '{{ request.user.name }}',
                        start: Date.parse(data[i].startTime, "yyyy-MM-dd HH:mm:ss"),
                        end: Date.parse(data[i].endTime, "yyyy-MM-dd HH:mm:ss"),
                        allDay: false
                    });
                }

上面的内容存在于正在呈现的模板中(我认为是正确的)。

调用函数 user_timetable 的 url conf 是

   url(r'^books/personal/(?P<userid>\d+)/timetable/$',twobooks.ajax.views.user_timetable),

但是,由于某种原因,user_timetable 没有被调用。

有人可以帮忙吗?

编辑- 好吧,原来的问题是模板没有正确渲染,因为 firebug 中的 url 是 '/books/personalNone/timetable/' ,这是不正确的。

我正在像这样渲染模板 -

def renderTimetableTemplate(request):
    #if request.POST['action'] == "personalTimetable":
    user = request.user
    return render_to_response(
        'books/personal.html',
        {
        'user': user,
        },
        context_instance = RequestContext(request)
    )

这有错误吗?

Ok, so I'm trying to call the function

def user_timetable(request, userid):
    user = get_object_or_404(TwobooksUser,id = userid)
    timeSlots = TimeSlot.objects.filter(user = request.user)
    rawtimeslots = []
    for timeSlot in timeSlots:
        newSlot = {
            'userid': timeSlot.user.id,
            'startTime': str(timeSlot.startTime),
            'endTime': str(timeSlot.endTime),
        }
        rawtimeslots.append(newSlot)
    return HttpResponse(simplejson.dumps(rawtimeslots))

through the javascript in

{% include 'elements/header.html' %}

    <script type='text/javascript'>

        $(document).ready(function() {

            $.get('/books/personal{{ user.id }}/timetable/', {}, function(data) {

                data = JSON.parse(data);
                var events = new Array();
                for (var i in data) {
                    events.push({
                        id: data[i].id,
                        title: '{{ request.user.name }}',
                        start: Date.parse(data[i].startTime, "yyyy-MM-dd HH:mm:ss"),
                        end: Date.parse(data[i].endTime, "yyyy-MM-dd HH:mm:ss"),
                        allDay: false
                    });
                }

where the above exists in a template that's being rendered (I think correctly).

The url conf that calls the function user_timetable is

   url(r'^books/personal/(?P<userid>\d+)/timetable/

But, user_timetable isn't being called for some reason.

Can anyone help?

EDIT-
Ok the original problem was that the template was not being rendered correctly, as the url in firebug comes to '/books/personalNone/timetable/' , which is incorrect.

I'm rendering the template like this -

def renderTimetableTemplate(request):
    #if request.POST['action'] == "personalTimetable":
    user = request.user
    return render_to_response(
        'books/personal.html',
        {
        'user': user,
        },
        context_instance = RequestContext(request)
    )

Is there a mistake with this?

,twobooks.ajax.views.user_timetable),

But, user_timetable isn't being called for some reason.

Can anyone help?

EDIT-
Ok the original problem was that the template was not being rendered correctly, as the url in firebug comes to '/books/personalNone/timetable/' , which is incorrect.

I'm rendering the template like this -

Is there a mistake with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2024-12-12 23:34:40

一句,“个人”后面缺少一个斜杠

  $.get('/books/personal{{ user.id }}/timetable/', {}, function(data) {

顺便说

  $.get('/books/personal/{{ user.id }}/timetable/', {}, function(data) {

。您应该使用 {% url %} 模板标记。

There is a slash missing after "personal"

  $.get('/books/personal{{ user.id }}/timetable/', {}, function(data) {

should be

  $.get('/books/personal/{{ user.id }}/timetable/', {}, function(data) {

Btw. you should use the {% url %} template tag.

浪荡不羁 2024-12-12 23:34:40

您转换为 JSON 并传递给脚本的数据与脚本所需的数据不匹配。您在每个时间段中传递一个 userId 元素,而脚本只需要 id

此错误应该显示在浏览器的 Javascript 控制台中,并且在 Firebug(或 Chrome 的内置开发人员工具)中更容易看到。

There is a mismatch between the data you're converting to JSON and passing to the script, and the data that the script is expecting. You are passing a userId element in each timeslot, whereas the script is expecting just id.

This error should have shown up in your browser's Javascript console, and would be even easier to see in Firebug (or Chrome's built-in Developer Tools).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文