- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 django 3 构建了一个网站,我将其更新到 django 4,因为此更新我收到此错误我需要帮助解决这个问题,谢谢
“WSGIRequest”对象没有属性“is_ajax”
这是我的观点
views.py
def SingleBlog(request, Slug):
post = get_object_or_404(Blog, Slug=Slug, Status="p")
comments = BlogComment.objects.filter(Post=post, Reply=None, Status="p")
is_liked = False
if post.Like.filter(id=request.user.id).exists():
is_liked = True
if request.method == 'POST':
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid:
content = request.POST.get('Text')
reply_id = request.POST.get('comment_id')
comment_qs = None
if reply_id:
comment_qs = BlogComment.objects.get(id=reply_id)
comment = BlogComment.objects.create(Post=post, User=request.user, Text=content, Reply=comment_qs)
comment.save()
# return HttpResponseRedirect(post.get_absolute_url())
else:
comment_form = CommentForm()
context = {
'object': post,
'comments': comments,
'is_liked': is_liked,
'comment_form': comment_form,
}
if request.is_ajax():
html = render_to_string('partial/comments.html', context, request=request)
return JsonResponse({'form': html})
return render(request, 'blog-single.html', context)
def post_like(request):
# post = get_object_or_404(Blog, id=request.POST.get('post_id'))
post = get_object_or_404(Blog, id=request.POST.get('id'))
is_liked = False
if post.Like.filter(id=request.user.id).exists():
post.Like.remove(request.user)
is_liked = False
else:
post.Like.add(request.user)
is_liked = True
context = {
'object': post,
'is_liked': is_liked,
}
if request.is_ajax():
html = render_to_string('partial/like_section.html', context, request=request)
return JsonResponse({'form': html})
谢谢
最佳答案
request.is_ajax
自 django 3.1 起已弃用,自 django 4 起已被删除。您可以使用
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
而不是
如果 request.is_ajax:
关于django - “WSGIRequest”对象在 django 4 中没有属性 'is_ajax',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71988457/
我尝试用ajax构建一个小日志。我遵循了一个指南,但他在 php 中构建时没有类和函数。当我尝试通过将其放入类和函数中来重建整个应用程序时,我收到此错误: undefined index :is_aj
我有一个 Django 应用程序,它根据页面请求是否为 Ajax 返回不同的响应。 当我查看一个页面,导航到其他地方,然后单击后退按钮时,请求被呈现为好像 request.is_ajax() 的值为
我编写了一个 Django View ,它根据 request.is_ajax() 响应 text/html 或 application/json。到目前为止一切顺利,但是当我使用浏览器历史记录按钮时
HttpRequest.is_ajax()从 3.1 版开始不推荐使用。 如果页面是从浏览器请求的,我想返回 html 并且作为 JsonResponse如果从 javascript 或以编程方式调用
我正在尝试在模板中进行条件扩展: {% extends request.is_ajax|yesno:"base_ajax.html,base.html" %} 在我看来: def new(reques
我知道已经有很多人问过这个问题,但我无法解决它。请看一下: 从配置文件 View 中,当按下“更新”按钮时,我希望向用户显示一个可编辑的表单,其中包含从数据库中选取的值。所以目前还没有 POST 数据
我正在用 Django + jQuery 测试 ajax。我的看法是这样的: def ajax_test(request): if request.is_ajax(): mes
问题:如何重定向到 Django 中的页面,但将其“伪造”为 ajax 请求? 所以我有一个发布数据请求 - 通过 ajax (jQuery) 完成。这被发送到我的 django 服务器,它添加数据(
Kohana 如何确定请求是否为 AJAX 请求? referrer 字符串有什么不同吗?我是否需要添加 GET 参数,也许 ?ajax=true ? 最佳答案 它会检查请求是否由 XMLHttpRe
这个问题已经有答案了: AttributeError: 'WSGIRequest' object has no attribute 'is_ajax' (3 个回答) 已关闭去年。 我用 django
我正在学习在 Django 中使用 Ajax,许多教程只是检查 request.method == 'GET' 或 POST。我很好奇我们需要什么 .is_ajax() 。不使用它是正常现象还是教程只
我正在发送跨域 ajax 请求,而在另一端 request.is_ajax() 给出了 False var text = getSelectedText(); text = 'text'; i
上下文: Chrome 浏览器扩展使用 JQuery 请求远程 django 应用程序的响应。 Django 识别出该请求是通过 AJAX 发出的,并以“Hello AJAX!”进行响应。我的锻炼基于
我发现 Django 的 request.is_ajax 是一种非常有用的方法,可以通过 JS 添加渐进式增强,并且在我的观点中仍然保持 DRY。 但是,如果 request.is_ajax,我想使用
我的 Django View 中有这段代码: if request.is_ajax(): if request.method == 'POST': data = json.lo
我正在将 Ajax 与 Django 结合使用。数据被进一步传输,但不被识别为 Ajax。浏览器 Chrome/Firefox; 为了检查请求是否通过ajax完成,我使用: `request.is_a
我是一名优秀的程序员,十分优秀!