gpt4 book ai didi

javascript - 对 django View 的 Ajax 请求未返回响应

转载 作者:行者123 更新时间:2023-12-01 02:32:39 24 4
gpt4 key购买 nike

我正在制作一个网站,我想要加载用户提交的对元素的评论,并且我希望能够动态显示它们,而不是让网页必须一次加载它们。所以我设置了这个ajax请求功能:

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

以及响应该请求的 View :

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)

但我没有得到回复。我在浏览器控制台中没有看到任何错误,但是在 django runserver 终端中我看到:

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'

或者如果我将 begin 和 end 设置为固定值而不是 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.

我尝试从“obtain_comments” View 中删除元素标识符,以便我可以在数据库中获取任何评论。不过,我希望该 View 能够响应我手动插入数据库的 1 条评论。

urls.py中我定义了这样的url:

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

为什么我没有收到回复?

最佳答案

View 中的

argskwargs 用于在 URL 中传递并在 URL 模式中捕获的参数。这些你都没有;您的 Ajax 正在发送 GET 查询字符串中的值。因此,您需要从 request.GET 获取它们。另请注意,这些值将是字符串,因此您需要将它们转换为整数。

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

另一个错误在错误消息本身中进行了解释;切片后不能订购。你应该先这样做。

关于javascript - 对 django View 的 Ajax 请求未返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com