博客
关于我
Django实现文章删除功能
阅读量:181 次
发布时间:2019-02-28

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

博客文章删除功能

一、操作思维导图

二、博客文章删除功能实现

  • 优化前端模板mysite/templates/article/column/article_list.html

    • 在模板中添加了一个删除按钮,按钮使用jQuery和Layer插件实现确认对话框,点击确定后通过AJAX发送POST请求。

    • 新增了以下脚本:

    function del_article(the, article_id) {    var article_name = $(the).parents("tr").children("td").eq(1).text();    layer.open({        type: 1,        skin: "layui-layer-rim",        area: ["400px", "200px"],        title: "删除文章",        content: '

    是否确定删除《'+article_name+'》

    ', btn: ['确定', '取消'], yes: function() { $.ajax({ url: '{% url "article:del_article" %}', type: "POST", data: {"article_id": article_id}, success: function(e) { if(e=="1") { parent.location.reload(); layer.msg("删除成功"); } else { layer.msg("删除失败"); } } }); } });}
  • 编写视图函数del_article

    • 函数使用@login_required装饰器确保用户登录后才能调用。

    • 函数通过article_id参数获取对应文章,调用delete()方法删除数据。

    • 处理异常情况,返回相应的HTTP响应状态码。

  • 配置URL路由

    • urls.py中添加了新的URL映射:
    url(r'^del-article/$', views.del_article, name="del_article"),
  • 三、测试

  • 删除前页面

    • 在文章列表页面,找到需要删除的文章,点击删除按钮,显示确认对话框。
  • 删除后页面

    • 点击确认后,系统会自动刷新页面,显示文章已被删除的提示信息。
  • 转载地址:http://uugj.baihongyu.com/

    你可能感兴趣的文章
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>