django构建API
model.py文件
- 这里不做展示
url.py文件
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.dashboard, name='show_dashboard'),
url(r'^show_content$', views.show_dashboard, name='show_content'),
]
views.py文件
from django.core import serializers
from django.shortcuts import render_to_response
from django.http import JsonResponse
from .models import controlInfo
def show_dashboard(request):
data = serializers.serialize("json", controlInfo.objects.all())
return JsonResponse(data, safe=False)
结果数据为
html
<script>
$.ajax({
method: 'GET',
url: "show_content" ,
dataType: 'json' ,
success: function(data){
console.log(data);
var s = data[0].fields;
console.log(s);
$("#result").html(s.title);
}
});
</script>