您好我正在尝试使用 AJAX 从我的数据库中显示主题名称。这是我现在的输出:
[{"pk": 1, "model": "school.subjects", "fields": {"name": "Math 140"}},
{"pk": 2, "model": "school.subjects", "fields": {"name": "English 102"}},
{"pk": 3, "model": "school.subjects", "fields": {"name": "CS210"}}]
但我只想显示:我该怎么做?
Math 140
English 102
CS210
这就是我的看法:
@csrf_exempt
def subjects_list(request):
if request.is_ajax():
user = request.user
subjects = Subjects.objects.filter(user__exact = user)
result = serializers.serialize("json", subjects, fields=('name'))
else:
result = "blablabl"
return HttpResponse(result)
这就是我的 test.html
{% extends "base.html" %}
{% block main-menu %}
<div id="result"></div>
<script type="text/javascript">
$(function() {
$.get("/subjects-list", function(data){
$("#result").append(data);
});
});
</script>
{% endblock %}