0

I'm making a website where I want to load user submitted comments on elements and I want to be able to dynamically display them and not have the webpage have to load them all at once. so I setup this ajax request function:

function loadcomments(){
   $.ajax({
        url: '/ajax/getcomments/',
        data: {
            'identifier': {{ identifier }}
            'begin': 0,
            'end': 30
        },
        dataType: 'json',
        success: function (data) {
            alert(data.comments);
        }
   });
};

And a view to respond to that request:

def obtain_comments(request, *args, **kwargs):
    begin = kwargs.get('begin');
    end = kwargs.get('end');
    comments = end - begin
    all_before = Comment.objects.order_by('-uploaded')[:end]
    data = {
        'comments': all_before.order_by('uploaded')[:comments]
    }
    return JsonResponse(data)

But I'm not getting a response. I'm not seeing any errors inside the browser console, however in the django runserver terminal I see:

Internal Server Error: /ajax/getcomments/
Traceback (most recent call last):
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/trie/Desktop/django/vidmiotest/player/views.py", line 64, in obtain_comments
    comments = end - begin
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

or if I set begin and end to a fixed value instead of kwargs:

Internal Server Error: /ajax/getcomments/
Traceback (most recent call last):
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/trie/Desktop/django/vidmiotest/player/views.py", line 67, in obtain_comments
    'comments': all_before.order_by('uploaded')[:comments]
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 1012, in order_by
    "Cannot reorder a query once a slice has been taken."
AssertionError: Cannot reorder a query once a slice has been taken.

I tried removing the element Identifier from the 'obtain_comments' view so I would get any comments in the database. I'm however expecting the view to respond 1 comment I manually inserted into the database.

inside urls.py i defined the url like this:

path('ajax/getcomments/', obtain_comments),

Why am I not geting a response?

2
  • So do you know why you're getting NoneType for end and/or begin? Commented Jan 10, 2018 at 20:57
  • No that's part of the question... I know the request gets to the view but i don't understand the errors that occure then Commented Jan 10, 2018 at 21:05

1 Answer 1

2

args and kwargs in a view are for arguments that are passed in the URL and captured in the URL pattern. You don't have any of these; your Ajax is sending the values in the GET querystring. So you need to get them from request.GET. Also note, the values will be strings, so you need to convert them to ints.

begin = int(request.GET['begin'])
end = int(request.GET['end'])

The other error is explained in the error message itself; you can't order after slicing. You should do it before.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.