【问题标题】:How to get field from json object in template?如何从模板中的json对象获取字段?
【发布时间】:2012-06-15 17:23:19
【问题描述】:

我在 django 中使用 Dajaxice 库。 这些是我的 ajax.py 方法:

def get_by_date_queryset():
#return model.objects.latest('created_on')
return Article.objects.all().order_by('created_on')

def example1(request):
""" Handling sorting requests """
jsonList = []
queryset = get_by_date_queryset()
json_serializer = serializers.get_serializer("json")()
return json_serializer.serialize(queryset, ensure_ascii=False)

有HTML:

<script>
function my_js_callback(data){
    alert(data) //this gives me [object Object],[object Object],[object Object]
    alert(data[0]) //this gives [object Object]
    alert(data[0].title) // fail

}
</script>
<button type="button" onclick="Dajaxice.content.example1(my_js_callback);">Click Me!</button>

虽然我无法从对象中获取我的字段...请帮助

【问题讨论】:

    标签: django json simplejson dajaxice


    【解决方案1】:

    这是我的错。我的 JSON 就像几个级别的字典,我找不到我的字段,因为它在 JSON 对象第一级字典中的字典命名字段中。现在好了:

    var key="title"
    var item=data["fields"]
    alert(item[key])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多