zoukankan      html  css  js  c++  java
  • Django -- 图书管理系统

    12.2.8 5 图书管理系统
    1. 模板语法
    {{ all_publisher }} 表示变量
    {% for publisher in all_publisher %}  # 循环体
        {{ forloop.counter }} 
        {{ publisher }}
    {% endfor %}
    2. 展示
    1. 从数据库中查询所有的数据

    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>

     

     

  • 相关阅读:
    Delphi WinAPI GetWindowRect
    Delphi WMI[2] 响应网线断开
    打印两个升序链表中共同的数据
    判断一个链表是否是回文结构
    删除有序数组中的重复项
    三数之和、最接近目标值的三数之和
    删除链表倒数第N个节点
    判断回文数
    字符串转整数
    整数反转
  • 原文地址:https://www.cnblogs.com/Agoni-7/p/11402895.html
Copyright © 2011-2022 走看看