Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Django übergibt JSON-Daten an statisches getJSON/Javascript

Haupt-HTML-Rendering + JSON-Daten

import json
from django.shortcuts import render

def startpage(request):
    platforms = Platform.objects.select_related().values('platformtype')
    return render(request, 'Main.html', {'platforms_as_json': json.dumps(list(platforms)),})

in Vorlage

{{ platforms_as_json }}

html und js

<select id="platformList"></select>

<script>
    $.each({% autoescape off %}{{platforms_as_json}}{% endautoescape %}, function (index, item) {
        $('#platformList').append(
                $('<option></option>').val(item.platformtype).html(item.platformtype)
        )
    });
</script>

Altes Beispielhttps://gist.github.com/leotop/014a38bd97407a6380f2526f11d17977