kaynak:
http://stackoverflow.com/questions/29835017/django-passing-apis-json-to-template-for-use-in-a-table
örnek:Bir json verisini tempalete(html) de <ul> veya <table> i içeride doğrudan nasıl gösterebilirim:
Normalde json çıktısı HttpResponse ile template aktarılır,bu durumda ajax kullanmak zorundasın ve verileri success: fonksiyonu içinde table vb. elemenleri tanımlayarak form üzerinde gösterebiliyorsun.Peki doğrudan {{ obj.id }} django tagı kullanarak nasıl gösterebilirim:
j1.txt içinde json verisi:
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
}
urls.py
------------
url(r'^a2/$',job.views.a2),
job/views.py:------------------
from django.shortcuts import render from .forms import ProfileForm from .models import Profile, Deneme from web2.settings import BASE_DIR from django.http import HttpResponse import urllib import json def a2(request): data_file = BASE_DIR+"/j1.txt" with open(data_file) as data_file: data = json.load(data_file) return render(request, 'a1.html',{'objects': data['employees']})
template/a1.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for obj in objects %} <li>{{ obj.firstName }} - {{ obj.lastName }}</li> {% endfor %} </ul> </body> </html>
<table> içinde göstermek için:
template/a3.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1px"> <thead> <th>name</th> <th>aaa</th> </thead> {% for obj in objects %} <tbody></tbody> <tr> <td>{{ obj.firstName }} </td> <td> {{obj.lastName }} </td> </tr> </tbody> {% endfor %} </table> </body> </html>
ÖRNEK:
{
"users": {
"tim": {
"username": "tim",
"first_name": "Tim",
"last_name": "Tom",
"id": 2
},
"bob": {
"username": "bob",
"first_name": "Jim",
"last_name": "Bob",
"id": 3
},
"bhaskar": {
"username": "bhaskar",
"first_name": "Bhaskar",
"last_name": "Rao",
"id": 1
}
}
}
gibi alt değerler içeren bir json çıktısını nasıl table'de gösterebiliriz?
users.txt içeriği:
{"users":
{"zeki": {"first_name": "zeki", "id": 3, "username": "zeki", "last_name": "sezer"},
"admin": {"first_name": "", "id": 1, "username": "admin", "last_name": ""},
"ali": {"first_name": "ali", "id": 2, "username": "ali", "last_name": "can"}
}
}
{"users":
{"zeki": {"first_name": "zeki", "id": 3, "username": "zeki", "last_name": "sezer"},
"admin": {"first_name": "", "id": 1, "username": "admin", "last_name": ""},
"ali": {"first_name": "ali", "id": 2, "username": "ali", "last_name": "can"}
}
}
urls.py
----------------
url(r'^test/$',job.views.test),
views.py
-------------------------
def test(request): data_file = BASE_DIR+"/users.txt" with open(data_file) as data_file: data = json.load(data_file) return render(request, 'users.html',{'objects': data['users']})template/users.html:-----------------<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Users</title> </head> <body> {{ objects }} <br><br> <table border="1px"> <thead> <th>id</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </thead> {% for obj in objects.values %} <tbody></tbody> <tr> <td>{{ obj.id }}</td> <td>{{ obj.first_name }}</td> <td>{{ obj.last_name }}</td> <td>{{ obj.username }}</td> </tr> </tbody> {% endfor %} </table> </body> </html>![]()



Hiç yorum yok:
Yorum Gönder