Django render_to_string无效(Django 1.4)

时间:2018-06-04 13:17:55

标签: python django

我正在尝试使用Django从ajax搜索返回结果集。我希望这个结果集显示在模板中,然后我希望模板在成功的django响应后返回。

    context ={}
    if request.method == 'POST':
        search_text = request.POST.get('search')
        monitors = RaspPiMonitor.objects.filter(
                     mac_address__icontains=search_text)
        context={
            'monitors':monitors,
        }

        html = render_to_string('realtime_reports/search_results.html', context)
        return HttpResponse(html)

    return render(request,'realtime_reports/monitors.html',context)

1 个答案:

答案 0 :(得分:0)

我刚刚更换了

html = render_to_string('realtime_reports/search_results.html', context)

使用

render(request, 'realtime_reports/search_results.html', context)