ajax删除,

ajax删除就是在html页面的script里写

getstudent.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div><a href="/add_student">添加</a></div>
<div> <table border="1">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>班级</th>
        <th>操作</th>
    </tr>
</thead>
    <tbody>
    {% for row in stulist %}
        <tr nid="{{ row.id }}">
        <td>{{ row.id }}</td>
        <td>{{ row.username }}</td>
        <td>{{ row.age }}</td>
            <td>{{ row.gender }}</td>
            <td>{{ row.cs.title }}</td><!-- 另一张表的内容-->

        <td><a href="del_student?nid={{ row.id }}">删除</a>
             |
            <a href="#"onclick="removeStudent(this)">ajax删除</a>
            |
        <a href="edit_student?nid={{ row.id }}">修改</a>
        </td>
        </tr>
    {% endfor %}
    </tbody>
</table></div>
<script src="/static/jquery-3.1.1.js"></script>
<script>
    function removeStudent(ths) {
        var nid=$(ths).parent().parent().attr('nid');
        {#alert(nid)#}
        $.ajax({
            url:'ajax4',
            type:'GET',
            data:{'nid':nid},
            success:function (arg) {
                if (arg=='s'){
                    //window.location.reload()
                    $(ths).parent().parent().remove()
                }else {
                    alert(arg)
                }

            }
        })
    }
</script>
</body>
</html>

url

from django.conf.urls import url
from django.contrib import admin
from app.views import classes
from app.views import student
from app.views import ajax
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^classes.html$',classes.get_classes),#不加^访问点击页面没反应
    url(r'^add_classes.html$',classes.add_classes),
    url(r'^del_classes.html$', classes.del_classes),
    url(r'^edit_classes.html$',classes.edit_classes),
    url(r'^set_teacher.html$', classes.set_teacher),

    url(r'^student$', student.get_student),
    url(r'^add_student$', student.add_student),
    url(r'^del_student$', student.del_student),
    url(r'^edit_student$',student.edit_student),
    url(r'^ajax1$', ajax.ajax1),
    url(r'^ajax2$', ajax.ajax2),
    url(r'^ajax3$', ajax.ajax3),
    url(r'^ajax4$', ajax.ajax4),

]

ajax.py

from django.shortcuts import render,HttpResponse
from app import models

def ajax1(req):
    return render(req,'ajax1.html')

def ajax2(req):
    user=req.GET.get('username')
    pwd=req.GET.get('password')
    import time
    time.sleep(5)
    return HttpResponse('你好')
def ajax3(req):
    v1=req.POST.get('a1')
    v2=req.POST.get('a2')
    try:
        v3=int(v1)+int(v2)
    except Exception as e:
            v3='格式错误'
    return HttpResponse(v3)

def ajax4(req):
    nid=req.GET.get('nid')
    msg='s'
    try:
        models.Student.objects.filter(id=nid).delete()
    except Exception as e:
        msg=str(e)
    return HttpResponse(msg)
原文地址:https://www.cnblogs.com/wfl9310/p/9593467.html