Django -- 圖書管理系統

12.2.8 5 圖書管理系統
1. 模板語法
{{ all_publisher }} 表示變量
{% for publisher in all_publisher %}  # 循環體
    {{ forloop.counter }} 
    {{ publisher }}
{% endfor %}
2. 展現
  1. 從數據庫中查詢全部的數據html

  2. 返回一個頁面數據庫

def publisher_list(request):
    # 查詢全部的數據(數據庫中)
    all_publisher = models.Publisher.object.all() # 獲取全部數據的對象列表
    return render(request,'publisher_list',{'all_publisher': all_publisher})   # 以字典的方式將數據對象列表傳到模板中,經過 . 的方式獲取值
    return render(request, 'publisher_list.html', locals()) # locals:當前局部名稱空間的變量,是一個字典(不用這種方式)
3. 新增
def publisher_add(request):
    if request.method == 'GET':   # GET請求跳轉到添加頁面
        return render(request,'publisher_add.html')
    elif request.method == 'POST':  # POST請求,提交數據
        # 獲取提交的出版社名稱
        pub_name = request.POST.get('pub_name') # 經過字典的key獲取提交的值
        if not pub_name:
            return render(request,'publisher_add.html', {'pub_name': pub_name, 'error': '輸入不能爲空'})
        if models.Publisher.objects.filter(name=pub_name): # 判斷數據庫中的name值與查詢出來的pub_name值相等
            # 數據庫已存在該數據
            return render(request, 'publisher_add.html', {'pub_name': pub_name, 'error': '數據已存在'})
        # 把數據插入到數據庫中:方式一
        ret = models.Publisher.objects.create(name=pub_name)
        # print(ret, type(ret))
# 方式二
        # obj = models.Publisher(name=pub_name) # 本身生成一個對象
        # obj.save()   # 至關於提交
        # 跳轉至展現頁面
        return redirect('/publisher_list')
# 精簡版
def publisher_add(request):
    pub_name,error = '',''    # 設置兩個變量pub_name 和 error 爲空字符串
    if request.method == 'POST':
        # 獲取提交的出版社名稱
        pub_name = request.POST.get('pub_name') # 經過字典的key獲取提交的值
        if not pub_name:
            error = '輸入不能爲空'
        elif models.Publisher.objects.filter(name=pub_name):
            # 數據庫已存在該數據
            error = '數據已存在'
        else:
            # 把數據插入到數據庫中
            models.Publisher.objects.create(name=pub_name)
            # print(ret, type(ret))
# 跳轉至展現頁面
            return redirect('/publisher_list')
    return render(request, 'publisher_add.html', {'pub_name': pub_name, 'error': error})
4. 刪除
def publisher_del(request):
    # 刪除數據
    pk = request.GET.get('pk') # 獲取pk(pid)
    query = models.Publisher.objects.filter(pk=pk) # 經過pk(pid)判斷數據庫中是否有該pk(pid)對應的數據
    if not query:
        # 數據不存在
        return HttpResponse('要刪除的數據不存在')
    # 刪除數據
    query.delete()   # 經過queryset列表刪除pk(pid)對應的數據
    # query[0].delete() # 經過索引刪除一個對象
    # 跳轉展現頁面
    return redirect('/publisher_list/')
5. 編輯
def publisher_edit(request):
    error = ''
    # 查詢要編輯的對象
    pk = request.GET.get('pk')  # url上攜帶的參數
    obj = models.Publisher.objects.filter(pk=pk).first() # 獲取對象列表中的第一個對象
    if not obj:
        return HttpResponse('要編輯的對象不存在')
    # 判斷請求方式
    if request.method == 'POST':
        # 獲取新提交的數據,編輯原始對象
        pub_name = request.POST.get('pub_name')
        if not pub_name:
            error = '輸入不能爲空'
        elif models.Publisher.objects.filter(name=pub_name):
            error = '數據已存在'
        else:
            # 編輯原始對象
            obj.name = pub_name
            obj.save()
            return redirect('/publisher_list/')
    return render(request, 'publisher_edit.html', {'obj': obj, 'error': error})
6. HTML頁面佈局
<!-- 查詢展現頁面 -->
<a href="/publisher_add/">添加</a>
<table border="1">
    <thead>
    <tr>
        <th>序號</th>
        <th>ID</th>
        <th>出版社名稱</th>
        <th>操做</th>
    </tr>
    </thead>
    <tbody>
        {% for publisher in all_publisher %}  <!-- 循環體:開始 -->
        <tr>
            <td>{{ forloop.counter }}</td>    <!-- 循環計數 -->
            <td>{{ publisher.pk }}</td>       <!-- 獲取對象列表中的ID -->
            <td>{{ publisher.name }}</td>     <!-- 獲取對象列表中的name -->
            <td>
                <a href="/publisher_del/?pk={{ publisher.pk }}">刪除</a>
                <a href="/publisher_edit/?pk={{ publisher.pk }}">編輯</a>
                <!-- ?pk={{ publisher.pk }} 將須要刪除或編輯的pid傳過來 -->
            </td>
        </tr>
        {% endfor %}                           <!-- 循環體:結束 -->
    </tbody>
</table>
<!-- 添加頁面 -->
<form action="" method="post">
    <p>
        出版社名稱: <input type="text" name="pub_name" value="{{ pub_name }}">  <!-- input框提交數據:以字典的格式 name:value -->
        <span style="color: red">{{ error }}</span>
    </p>
    <button>提交</button>
</form>
<!-- 編輯頁面 -->
<form action="" method="post">
    <p>
        出版社名稱: <input type="text" name="pub_name" value="{{ obj.name }}">  <!-- obj.name : 經過須要編的對象獲取其value值 -->
        <span style="color: red">{{ error }}</span>
    </p>
    <button>提交</button>
</form>
相關文章
相關標籤/搜索