博客
关于我
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/

    你可能感兴趣的文章
    Python: AES加密与解密
    查看>>
    Python: Range() 最大尺寸;动态的还是静态的?
    查看>>
    Python: Windows下用 multiprocessing 的深坑
    查看>>
    python:Could not find platform independent libraries <prefix>解决方法
    查看>>
    Python:datetime tzinfo 时区名称文档
    查看>>
    Python:deepcopy(list) vs new_list = old_list[:]
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
    查看>>
    Python:TypeError:无法连接“str“和“int“对象
    查看>>
    Python:XPath 在 ElementTree 中不可用
    查看>>
    Python:从 FTP 服务器下载文件
    查看>>
    python:从列表中创建元组列表
    查看>>
    Python:从嵌套列表中删除重复项
    查看>>
    Python:从间隔映射到值
    查看>>
    Python:使用 shell=False 的子进程调用不起作用
    查看>>
    Python:修改 PATH 环境变量的平台无关方式
    查看>>
    Python:假与 0
    查看>>
    Python:函数使用
    查看>>
    Python:函数进阶
    查看>>