【发布时间】:2018-11-21 12:54:20
【问题描述】:
当我在 js 文件中使用 console.log() 时,我的 Html 页面没有在 ajax 成功时呈现,HTML 页面在浏览器控制台中打印出来,而不是在浏览器本身中。
请检查我的以下代码:
views.py:
def Filter_by_Products(request):
if request.is_ajax():
if request.method == 'GET':
print("filter by function!")
filter_brand = request.GET.get('filter_brand')
products_qs, qs_color, qs_size = product.objects.Filter_brand(filter_brand.rstrip(','))
context={
"object_list": products_qs,
'colors': qs_color,
'sizes':qs_size
}
# print(products_qs)
# print(qs_color)
# print(qs_size)
return render(request,"product/products.html",context)
ajax.js:
$.ajax({
url:'/cart/api/filterby/',
method:'get',
data:
{
'filter_brand':str,
},
success: function (data) {
console.log(data)
// location.reload()
},
error: function (error) {
console.log(error)
}
})
在谷歌搜索我的问题后,我发现我的问题是我试图将服务器端与客户端混合,解决方案是使用 HttpResponse 而不是渲染函数发送我的 HTML 模板,然后使用 javascript,我选择 HTML 页面并使用新的 html 更改内容。
实际上,我并不清楚问题以及为什么将服务器与客户端混合会导致该问题以及为什么我的代码从第一次使用渲染时就无法正常工作,所以请您详细解释一下或参考链接给我理解。
另外,我对 Django celery 和 Redis 很熟悉,如果我可以在那种情况下使用这些技术,你能告诉我如何使用它们吗?
【问题讨论】:
标签: python django django-templates