1

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',
        {
        'user': user,
        },
        context_instance = RequestContext(request)
    )

Is there a mistake with this?

scubadiver
  • 71
  • 1
  • 4
  • 1
    Did you looked at the ajax request using firebug or something. Have a look at the source of your page (in the browser) and check the URL of `$.get()`. – Reto Aebersold Sep 20 '11 at 15:17
  • ok, I just did that and I think I found the problem - $.get('/books/personalNone/timetable/') is what is happening. Which means that I'm not rendering the template correctly, but I'm not sure what mistake I'm making- can you look at the edited code above? – scubadiver Sep 20 '11 at 15:20

2 Answers2

0

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.

schneck
  • 10,556
  • 11
  • 49
  • 74
  • what other problem could it be? – scubadiver Sep 20 '11 at 15:29
  • in fact, I did a print "if working" in the function user_timetable, and it's not printing, which means the function is not being called – scubadiver Sep 20 '11 at 15:31
  • please copy the url you request into your browser and see what the response is. maybe you have an error in your view. – schneck Sep 20 '11 at 15:36
  • er, it returns a list of TimeSlot objects – scubadiver Sep 20 '11 at 15:40
  • what shows up in firebug, then? look at the request and response data. do they match the format you except on processing the data? – schneck Sep 20 '11 at 15:44
  • ok, I put in alerts in teh javascript and they're not showing, suggesting that teh template's not being rendered- can you see any syntax errors, or any other errors? – scubadiver Sep 20 '11 at 15:55
  • as told several times (also by others): use firebug. you can see javascript errors and debug the ajax-interaction. – schneck Sep 20 '11 at 16:02
0

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

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895