django前端删除与修改按钮代码

版权声明:学习是一种信仰。喜欢就拿去,送人玫瑰手有余香。 https://blog.csdn.net/huoyuanshen/article/details/83545329

1,前端样式:

2,前端代码:

2.1文件下载与引入:

https://github.com/qiwsir/DjangoPracticeProject/tree/master/mysite2/static/js 

layer.js与skin目录复制到本地项目./static/js

2.2前端代码:

{% load staticfiles %}

<td><a name="edit" href="javascript:" onclick="edit_column(this,{{ column.id }},{{ column.column }})"><span class="glyphicon glyphicon-pencil"></span></a>
<a name="delete" href="javascript:" onclick="del_column(this,{{ column.id }},{{ column.column }})"><span class="glyphicon glyphicon-trash" style="margin-right: 20px;"></span></a>
</td>
<script type="text/javascript" src="{% static "js/jquery.js" %}"></script>
<script type="text/javascript" src="{% static "js/layer.js" %}"></script>
<script type="text/javascript">
function edit_column(the,column_id,column_name){
{#        var name = $(the).parent("tr").children("td").eq(1).text(); 另一种方法#}
        var index = layer.open({
            type:1,
            skin:"layui-layer-rim",
            area:["400px","200px"],
            title:"编辑栏目",
            content:'<div class="text-center" style="margin-top:20px"><p>请输入新的栏目名称</p>'+
            '<p><input type="text" id="new_name" value="'+column_name+'"></p></div>',
            btn:['确定','取消'],
            yes: function(index,layero){
                column_new_name = $('#new_name').val();
                $.ajax({
                    url: '{% url 'article:rename_article_column' %}',
                    type:'POST',
                    data: {'column':column_new_name,'column_id':column_id},
                    success:function (e){
                    if(e=="1"){
                        parent.location.reload();
                        layer.msg("good");
                    }else{
                        layer.msg("新名称没有保存,修改失败。");
                    }
                }
                });
            },
            btn2: function(index,layero){
                layer.close(index);
            }
        }
        )
    }
</script>

3,后端代码

@csrf_exempt
@require_POST
@login_required(login_url='/account/login/')
def rename_column(request):
    column_name = request.POST['column']
    column_id = request.POST['column_id']
    try :
        line = ArticleColumn.objects.get(id=column_id)
        line.column = column_name
        line.save()
        return HttpResponse(1)
    except:
        return HttpResponse(0)

猜你喜欢

转载自blog.csdn.net/huoyuanshen/article/details/83545329