开发者

Django Jquery Get URL Conf

开发者 https://www.devze.com 2023-04-05 23:49 出处:网络
Ok, so I\'m trying to call the function def user_timetable(request, userid): user = get_object_or_404(TwobooksUser,id = userid)

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/$',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 -

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

Is there a mistake with this?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消