【问题标题】:Show html generated by using json2html at webpage [Django] [Python]?在网页 [Django] [Python] 上显示使用 json2html 生成的 html?
【发布时间】:2016-09-05 10:36:52
【问题描述】:

以下是我的代码:

 RESULTS= {}
 for k in RESULTS_LIST[0].iterkeys():
   RESULTS[k] = list(results[k] for results in RESULTS_LIST)
 RESULTS.pop('_id',None)
 html_table =  json2html.convert(json=RESULTS)
 return render(request,'homepage.html',{'html_table':html_table})

在这里,我在名为 RESULTS 的 JSON 中安排从 Mongo Db 获取的数据,并通过使用 JSON2HTML 包,成功地为 JSON 中给出的数据生成了 html 表。要将html表格代码嵌入html页面的空白部分,我正在做:

<div>{{html_table}}</div>

但它无法在页面上显示表格。我尝试了很多方法来实现它,但都没有成功。请帮我解决这个问题。如果有任何相关的例子,你以前做过,那么请引导我朝着正确的方向前进。

JS代码为:

angular.module("homeapp",[])
.controller("homecontroller",['$http','$scope',function($http,$scope){
    $scope.search= {
        'keyword': null,
        'option': null,
        'startDate':null,
        'lastDate': null
    };
    $scope.search_info=function(){
        var search_req = {
            url:'/home',
            method:'POST',
            data:$scope.search
        }
        $http(search_req) //in case I dont want any response back or
        $http(search_req).success(function(response){     
            window.alert(response)
         }) //in case I need to check response
    }

}]);

【问题讨论】:

  • 您是否仔细检查了html_table 变量是否实际包含任何内容?
  • 是的@danidee,我已经检查了发回给 Ajax 调用的响应。我也打印了变量,我将它存储在其中。表格代码生成成功,里面的数据整洁干净。
  • 如果您使用 Ajax 调用来渲染它,您应该显示该 JS 代码。
  • @Daniel Roseman 请检查我们是否可以通过 JS 来完成,因为我是第一次使用 Angular JS。

标签: python json django


【解决方案1】:

我已经通过使用 js 以一种简单的方式解决了这个问题,下面给出了它并且它有效。

    $http(search_req).success(function(response){     
        angular.element(".search_results").append(response);
     })

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2012-04-19
    • 1970-01-01
    • 2018-09-16
    • 2015-09-03
    • 2021-12-06
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多