2012-02-11 2 views
3

내 API를 더 잘 디버깅하기 위해 json obj의 모든 응답을 한 줄씩 보여주는 디버그 페이지를 만들고 싶습니다. 코드 또는 장고 템플릿으로 할 수 있다고 생각합니다. 가장 간단한 방법은 무엇입니까?django 템플릿을 사용하여 json을 html로 채우십시오

https://developers.facebook.com/tools/explorer/?method=GET 예를 들어, facebook explorer는 json obj와 같은 응답의 세부 정보를 나열합니다.

{ 
    "id": "xxx", 
    "name": "xxx", 
    "first_name": "xxx", 
    "last_name": "xxx", 
    "link": "xxx", 
    "username": "xxx", 
    "hometown": { 
    "id": "xxx", 
    "name": "xxx" 
    }, 
    "location": { 
    "id": "xxx", 
    "name": "xxx" 
    }, 
    "bio": "Drink Coffee in Whitehorse", 

    "work": [ 
    { 
     "employer": { 
     "id": "xxx", 
     "name": "xxx" 
     }, 
     "location": { 
     "id": "xxx", 
     "name": "xxx" 
     }, 
     "position": { 
     "id": "xxx", 
     "name": "xxx" 
     }, 
     "start_date": "2009-01", 
     "end_date": "0000-00" 
    }, 
} 

답변

8

당신은 꽤 인쇄에 의해 생성 된 JSON을 들여하는 공간의 수와 동일한 indent 키워드 인수 json.dumps()를 호출해야합니다. 예를 들어

:

json.dumps(spam_and_eggs, indent=4) 
2

나는 당신의 JSON 꽤 보인다 있도록 HTML에 <pre></pre>을 사용하는 것이 좋습니다 수 있습니다.

import json 
from django.contrib.admin.views.decorators import staff_member_required 
from django.shortcuts import render 
from .models import Scan 

    @staff_member_required 
    def info(request): 
     my_info = Scan.scan() 
     return render(request, 'info.html', {'info': json.dumps(my_info, indent=4)}) 

HTML :

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Title</title> 
    </head> 
    <body> 
     <pre>{{ info|safe }}</pre> 
    </body> 
</html> 
관련 문제