Django-sweetAlert

第一步:導(dǎo)入sweetalert.css與sweetalert.min.js文件


image.png
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>sweetalert_demo</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
</head>
<body>

</body>
   <script src="/static/jquery-3.3.1.js"></script>
   <script src="/static/bootstrap/js/bootstrap.min.js"></script>
   <script src="/static/sweetalert/sweetalert.min.js"></script>
</html>

第二步:
以person表為例,創(chuàng)建person顯示列表

  • html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>sweetalert_demo</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
</head>
<body>
<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Person管理后臺</h3>
    </div>
    <div class="panel-body">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>序號</th>
                <th>編號</th>
                <th>姓名</th>
                <th>年齡</th>
                <th>出生日期</th>
                <th>操作臺</th>
            </tr>
            </thead>
            <tbody>
            {% for p in persons %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td>{{ p.id }}</td>
                <td>{{ p.name }}</td>
                <td>{{ p.age }}</td>
                <td>{{ p.birth|date:"Y-m-d" }}</td>
                <td>
                    <button class="btn-danger"><i class="fa-trash-o">刪除</i></button>
                </td>
            </tr>
            {% endfor %}

            </tbody>
        </table>
    </div>
</div>
</body>
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/sweetalert/sweetalert.min.js"></script>
</html>
  • person.py
def person(request):
    ret = models.Person.objects.all()
    return render(request,"sweetalert_demo.html",{"persons":ret})

image.png

第三步:點擊刪除按扭,顯示是否要刪除的提示框敦间,這時要用到sweetAlert插件,插件下載地址:https://github.com/lipis/bootstrap-sweetalert

<script>
     $(".del").on("click",function () {
        swal('標(biāo)題','內(nèi)容',"success");
     })
</script>
image.png

調(diào)整padding:

 <style>
        div.sweet-alert>h2 {
            padding-top:15px;
        }
    </style>

swal()更多用法詳見https://lipis.github.io/bootstrap-sweetalert/

swal({
  title: "Are you sure?",
  text: "Your will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonClass: "btn-danger",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

對上述代碼進行修改

<script>
    $(".del").on("click", function () {
        swal({
                title: "你確定要刪除嗎炸渡?",
                text: "刪除了就找不回來了哦娜亿!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-warning",
                confirmButtonText: "確認(rèn)",
                cancelButtonText: "放棄",
                closeOnConfirm: false
            },
            function () {
                swal("Deleted!", "Your imaginary file has been deleted.", "success");
            });
    })
image.png

第四步:加上ajax使前后端關(guān)聯(lián),取到刪除的ID標(biāo)簽蚌堵。
ajax請求方式是post,所以引用stupajax.js文件
ajax 數(shù)據(jù)字典取得要刪除那行對應(yīng)的ID標(biāo)簽买决,這個ID標(biāo)簽與后臺del_person函數(shù)對應(yīng)的ID取得關(guān)聯(lián)。同時result參數(shù)傳遞到arg形參吼畏。

<script>
    $(".del").on("click", function () {
        var $trlie = $(this).parent().parent();
        var delid = $trlie.children().eq(1).text();
        swal({
                title: "你確定要刪除嗎督赤?",
                text: "刪除了就找不回來了哦!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-warning",
                confirmButtonText: "確認(rèn)",
                cancelButtonText: "放棄",
                closeOnConfirm: false
            },
            function () {
                $.ajax({
                    url:'/del_person/',
                    type:'post',
                    data:{"id":delid},
                    success:function (arg) {
                         swal(arg, "你已經(jīng)刪除掉了", "success");
                         $trlie.remove()
                    }
                });

            });


    })

</script>
  • html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>sweetalert_demo</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
    <style>
        div.sweet-alert > h2 {
            padding-top: 15px;
        }
    </style>
</head>
<body>
<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Person管理后臺</h3>
    </div>
    <div class="panel-body">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>序號</th>
                <th>編號</th>
                <th>姓名</th>
                <th>年齡</th>
                <th>出生日期</th>
                <th>操作臺</th>
            </tr>
            </thead>
            <tbody>
            {% for p in persons %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ p.id }}</td>
                    <td>{{ p.name }}</td>
                    <td>{{ p.age }}</td>
                    <td>{{ p.birth|date:"Y-m-d" }}</td>
                    <td>
                        <button class="btn-danger del"><i class="fa fa-trash-o">刪除</i></button>
                    </td>
                </tr>
            {% endfor %}

            </tbody>
        </table>
    </div>
</div>
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/sweetalert/sweetalert.min.js"></script>
<script src="/static/setupajax.js"></script>
<script>
    $(".del").on("click", function () {
        var $trlie = $(this).parent().parent();
        var delid = $trlie.children().eq(1).text();
        swal({
                title: "你確定要刪除嗎泻蚊?",
                text: "刪除了就找不回來了哦躲舌!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: "btn-warning",
                confirmButtonText: "確認(rèn)",
                cancelButtonText: "放棄",
                closeOnConfirm: false
            },
            function () {
                $.ajax({
                    url:'/del_person/',
                    type:'post',
                    data:{"id":delid},
                    success:function (arg) {
                         swal(arg, "你已經(jīng)刪除掉了", "success");
                         $trlie.remove()
                    }
                });

            });


    })

</script>

</body>
</html>
  • 后臺views.py
def person(request):
    ret = models.Person.objects.all()
    return render(request,"sweetalert_demo.html",{"persons":ret})

def del_person(request):
    del_id = request.POST.get('id')
    models.Person.objects.filter(id = del_id).delete()
    result = "后臺數(shù)據(jù)庫存已刪除"
    return HttpResponse(result)
image.png
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市性雄,隨后出現(xiàn)的幾起案子没卸,更是在濱河造成了極大的恐慌,老刑警劉巖秒旋,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件约计,死亡現(xiàn)場離奇詭異,居然都是意外死亡迁筛,警方通過查閱死者的電腦和手機病蛉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瑰煎,“玉大人铺然,你說我怎么就攤上這事【频椋” “怎么了魄健?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長插勤。 經(jīng)常有香客問我沽瘦,道長,這世上最難降的妖魔是什么农尖? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任析恋,我火速辦了婚禮,結(jié)果婚禮上盛卡,老公的妹妹穿的比我還像新娘助隧。我一直安慰自己,他們只是感情好滑沧,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布并村。 她就那樣靜靜地躺著巍实,像睡著了一般。 火紅的嫁衣襯著肌膚如雪哩牍。 梳的紋絲不亂的頭發(fā)上棚潦,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音膝昆,去河邊找鬼丸边。 笑死,一個胖子當(dāng)著我的面吹牛荚孵,可吹牛的內(nèi)容都是我干的妹窖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼处窥,長吁一口氣:“原來是場噩夢啊……” “哼嘱吗!你這毒婦竟也來了玄组?” 一聲冷哼從身側(cè)響起滔驾,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎俄讹,沒想到半個月后哆致,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡患膛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年摊阀,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片踪蹬。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡胞此,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出跃捣,到底是詐尸還是另有隱情漱牵,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布疚漆,位于F島的核電站酣胀,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏娶聘。R本人自食惡果不足惜闻镶,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丸升。 院中可真熱鬧铆农,春花似錦、人聲如沸狡耻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涛碑,卻和暖如春精堕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蒲障。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工歹篓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人揉阎。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓庄撮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親毙籽。 傳聞我的和親對象是個殘疾皇子洞斯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 用到的組件 1、通過CocoaPods安裝 2坑赡、第三方類庫安裝 3烙如、第三方服務(wù) 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,615評論 1 180
  • 前些日子從@張鑫旭微博處得一份推薦(Front-end-tutorial),號稱最全的資源教程-前端涉及的所有知識...
    谷子多閱讀 4,194評論 0 44
  • 第一部分 Python基礎(chǔ)篇(80題) 1毅否、為什么學(xué)習(xí)Python亚铁? Python相對于其他編程語言有很多優(yōu)點: ...
    清清子衿木子水心閱讀 1,707評論 0 1
  • 字符串 1.什么是字符串 使用單引號或者雙引號括起來的字符集就是字符串。 引號中單獨的符號螟加、數(shù)字徘溢、字母等叫字符。 ...
    mango_2e17閱讀 7,508評論 1 7
  • 《閉上眼睛才能看清楚自己》這本書是香海禪寺主持賢宗法師的人生體悟捆探,修行心得及講學(xué)錄然爆,此書從六個章節(jié)講述了禪修是什么...
    宜均閱讀 10,021評論 1 25