dajngo实现ajax异步刷新

前言

实现前后端分离,异步刷新

1.应用文件中配置路由urls.py

urlpatterns = [
    url(r'sajax/', views.sajax),
]
urlpatterns += router.urls

2.配置views.py

def sajax(request):
    if request.method == 'GET':
        return render(request, 'stuajax.html')

3.配置html




    
    展示所有学生
    
    


    
{#    #}
    
{% csrf_token %}

4.views.py中处理请求, 返回数据

def user_change_select(request):

    if request.method == 'POST':
        cart_id = request.POST.get('cart_id')
        user = request.user
        data = {
            'code': '200',
            'msg': '请求成功'
        }
        if user and user.id:
            cart = CartModel.objects.filter(pk=cart_id).first()
            if cart.is_select:
                cart.is_select = False
            else:
                cart.is_select = True
            cart.save()
            data['is_select'] = cart.is_select
        return JsonResponse(data)

你可能感兴趣的:(dajngo实现ajax异步刷新)