1. 程式人生 > >django前端刪除與修改按鈕程式碼

django前端刪除與修改按鈕程式碼

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)