1. 程式人生 > 實用技巧 >3.1.2 編輯欄目

3.1.2 編輯欄目

目錄

1.修改前端新增編輯按鈕 新增rename函式

vitemplates/article/column/article_column.html

{% extends "article/base.html" %}
{% load staticfiles %}
{% block title %}article column{% endblock %}
{% block content %}
<div>
    <p class ="text-right"><button class="btn btn-primary" id="add_column" onclick="add_column()"
value="abc">add column</button></p> <table class="table table-hover"> <tr> <td>序號</td> <td>欄目名稱</td> <td>操作</td> </tr> {% for column in columns %} <tr> <
td>{{ forloop.counter }}</td> <td>{{ column.column }}</t d> <td> <a name="edit" onclick="edit_column(this, {{ column.id }})"> <span class="glyphicon glyphicon-pencil"></span></a> <
a name="delete" onclick="del_column(this,{{ column.id }} )"> <span class="glyphicon glyphicon-trash" style="margin-left:20px"></span></a> </td> </tr> {% empty %} <p>還沒有設直欄目,太懶了。</p> {% endfor %} </table> </div> <script type="text/javascript" src='{% static "js/jquery-3.4.1.min.js" %}'></script> <script type="text/javascript" src="{% static 'js/layer.js' %}"></script> <script type="text/javascript"> function add_column(){ 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>{{column_forms.column}}</div>', btn:['確定','取消',], yes: function(index, layero ){ column_name =$('#id_column').val(); //alert(column_name); $.ajax({ url:'{% url "article:article_column" %}', type:'POST', data:{"column":column_name}, success:function (e){ if (e=="1"){ parent.location.reload(); layer.msg("good"); }else{ layer.msg("此欄目已有,請更換另一個"); } }, }); }, btn2:function(index, layero){ layer.close(index); } }); } function edit_column(the,column_id){ var name = $(the).parent().prev().text().trim(); 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="'+name+'"></p></div>', btn:['確定','取消',], yes: function(index, layero ){ new_name =$('#new_name').val(); //alert(column_name); $.ajax({ url:'{% url "article:rename_article_column" %}', type:'POST', data:{"column_id":column_id,"column_name":new_name}, success:function (e){ if (e=="1"){ parent.location.reload(); layer.msg("good"); }else{ layer.msg("修改失敗"); } }, }); }, //寫不寫btn2 取消按鈕都可以關閉 }); } </script> {% endblock %}
View Code

2.新增rename檢視

viarticle/views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import ArticleColumn
from .forms import ArticleColumnForm
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.views.decorators.http import require_POST

# Create your views here.
@login_required(login_url="/account/login/")
@csrf_exempt
def article_column(request):
    if request.method == "GET":
        columns = ArticleColumn.objects.filter(user=request.user)
        column_forms = ArticleColumnForm
        return render(request,"article/column/article_column.html",{"columns":columns,"column_forms":column_forms})
    if request.method == "POST":
        column_name = request.POST["column"]
        print(column_name)
        columns = ArticleColumn.objects.filter(user_id=request.user.id,column=column_name)
        if columns:
            return HttpResponse("2")
        else:
            ArticleColumn.objects.create(user=request.user,column=column_name)
            return HttpResponse("1")

@login_required(login_url='/account/login')
@require_POST
@csrf_exempt
def rename_article_column(request):
    column_name=request.POST["column_name"]
    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")
View Code

3.編輯url

viarticle/urls.py

from django.conf.urls import url
from . import views
from django.conf import settings

urlpatterns = [
    url(r'^article_column/$',views.article_column,name="article_column"),
    url(r'^rename_column/$',views.rename_article_column,name="rename_article_column"),
]
View Code