Skip to content

Instantly share code, notes, and snippets.

@leotop
Created July 27, 2016 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leotop/014a38bd97407a6380f2526f11d17977 to your computer and use it in GitHub Desktop.
Save leotop/014a38bd97407a6380f2526f11d17977 to your computer and use it in GitHub Desktop.
views.py
import json
from django.http import HttpResponse
def startpage(request):
return render_to_response('Main.html');
def index(request):
platforms = Platform.objects.select_related().values('platformtype')
return HttpResponse(json.dumps(list(platforms)))
# return HttpResponse(json.dumps(platforms)) # one value
js code
function GetPlagtform() {
$.ajax({
type: "GET",
url: "/api/platform.json",
contentType: "application/json;charset=utf-8",
dataType: "json",
cache: false,
success: function (result) {
$.each(result, function (i, value) {
$('#platformList').append(
$('<option></option>').val(value.platformtype).html(value.platformtype)
);
},
error: function (response) {
alert('error');
}
});
}
code not tested.
imitation google maps struct example
var data = {"offers": [{"address": "city 1", "name": "Apartment 1", "lat": "48.967", "code": "11111", "lng": "7.4639"}, {"address": "city 2", "name": "Apartment 2", "lat": "48.964", "code": "22222", "lng": "7.470"}, ...
func
def show_all_json_geo(request):
geo_obj = Offer.objects.select_related().values(
'name',
'address',
'lat',
'lng',
'code',
)
offers = {'offers': list(geo_obj)}
return HttpResponse("var data = " + json.dumps(offers)) # work, simple imitation.
html load data variable
<script src="/json/geo.json"></script>
in js code use as data
for (var i = 0; i < 100; i++) {
var dataPhoto = data.offers[i];
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment