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?
NoneTypeforendand/orbegin?