Ajax的使用
发布日期:2021-09-27 06:24:50 浏览次数:3 分类:技术文章

本文共 872 字,大约阅读时间需要 2 分钟。

问题:使用视图将上下文数据传递给模板,需要先加载模板,再渲染模板等操作,最后生成html代码再返回给浏览器。如此操作复杂且扩展性差,此时开发人员既写后台又写前端解决:使用ajax异步网络请求Json:存储数据的一种结构组成  {} 表示字典  [] 表示列表   :表示键值对  ,分隔两个部分作用  用于网络传输代码:urlpatterns = [    url(r'^index/$',views.index),    url(r'^students/$', views.students),from django.shortcuts import renderfrom myApp.models import Grade,Studentfrom django.http import HttpResponse,JsonResponse# Create your views here.def index(request):    return render(request,'myApp/index.html')def students(request):    if not request.is_ajax:       return HttpResponse("请求错误")    else:        ss = Student.objects.all()        objs = []        for item in ss:            obj = {}            obj["name"] = item.name            obj["age"] = item.age            objs.append(obj)    #JsonResponse会将数据进行序列化操作(将对象转化为字典),模型对象无法被直接转为字典        return JsonResponse({"ss":objs}){% load static %}    
主页

转载地址:https://blog.csdn.net/feiYu12138/article/details/83514022 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:报错 Application labels aren't unique, duplicates: myApp(应用名)
下一篇:xshell 5 过期解决方案

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月06日 22时02分03秒