• 图书管理系统(三):作者列表增加、删除和编辑


    • 步骤一:创建作者列表
    • 步骤二:添加作者
    • 步骤三:删除作者
    • 步骤四:编辑作者
     
     
    步骤一:创建作者列表

    1.先创建数据表
    class Author(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32, null=False, unique=True)
        book = models.ManyToManyField(to='Book')    #会自动生成第三方表,内容是id,author_id,book_id,然后添加数据
    2.写入函数:
    url(r'^author_list/', views.author_list),
    def author_list(request):
        datas = models.Author.objects.all()
        return render(request, 'author_list.html', {'datas': datas})
    3.写html文件:
    <!DOCTYPE html>
    <html>
    <head>
        <title>作者列表</title>
    </head>
    <body>
     
    <h1>作者列表</h1>
    <table border="1">
    <thead>
        <tr>
            <th>序号</th>
            <th>id</th>
            <th>作者</th>
            <th>作品</th>
        </tr>
    </thead>
    <tbody>
        {% for data in datas %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ data.id }}</td>
            <td>{{ data.name }}</td>
            <td>
            {% for book in data.book.all %}
                {% if forloop.last %}    #if判断,如果是倒数第二个,则不加;,否则加
                    {{ book.title }}
                {% else %}
                    {{ book.title }};
                {% endif %}
            {% endfor %}
            </td>
        </tr>
        {% endfor %}
    </tbody>
    </table>
    </body>
    </html>
     
    步骤二:添加作者

    1.写入函数:
    url(r'^add_author', views.add_author),
    def add_author(request):
        if request.method == 'POST':
            new_name = request.POST.get('add_name')
            new_books = request.POST.getlist('books')
            ret = models.Author.objects.create(name=new_name)
            ret.book.set(new_books)
            ret.save()
            return redirect('/author_list/')
        ret = models.Book.objects.all()
        return render(request, 'add_author.html', {'book_list': ret})
    2.写html文件:
    <!DOCTYPE html>
    <html>
    <head>
        <title>添加作者</title>
    </head>
    <body>
     
    <h1>添加作者</h1>
    <form action="/add_author/" method="post">
        作者:<input type="text" name="add_name">
     
        <select name="books" multiple='multiple'>
            {% for book in book_list %}
            <option value="{{ book.id }}">{{ book.title }}</option>
            {% endfor %}
        </select>
     
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    (遗留了一个问题:表单的多选没有生效)
     
    步骤三:删除作者

    1.写入函数:
    url(r'^delete_author', views.delete_author),
    def delete_author(request):
        ret = request.GET.get('id')
        models.Author.objects.get(id=ret).delete()
        return redirect('/author_list/')
    2.写入html文件:
    <td><a href="/delete_author/?id={{ data.id }}">删除</a></td>
     
    步骤四:修改作者

    1.写入函数:
    url(r'^edit_author/', views.edit_author),
    def edit_author(request):
        if request.method == 'POST':
            edit_id = request.POST.get('edit_id')
            print('------------')
            print(edit_id)
            print('------------------')
            ret = request.POST.get('edit_name')
            datas = request.POST.getlist('books')
            print('=!'*200)
            author = models.Author.objects.get(id=edit_id)
            print('='*23)
            author.name = ret
            author.book.set(datas)
            author.save()
            return redirect('/author_list/')
        ret = request.GET.get('id')
        data = models.Author.objects.get(id=ret)
        ret = models.Book.objects.all()
        return render(request, 'edit_author.html',{'book_list': ret, 'data':data})
    2.写html文件:
    <!DOCTYPE html>
    <html>
    <head>
        <title>编辑作者</title>
    </head>
    <body>
     
    <h1>编辑作者</h1>
    <form action="/edit_author/" method="post">
        <input type="text" name="edit_id" value='{{ data.id }}' style="display: none">
        作者:<input type="text" name="edit_name" value="{{ data.name }}">
     
        <select multiple name="books">
            {% for book in book_list %}
                {% if book in data.book.all %}
                    <option selected value="{{ book.id }}">{{ book.title }}</option>
                {% else %}
                    <option value="{{ book.id }}">{{ book.title }}</option>
                {% endif %}
            {% endfor %}
        </select>
     
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
     
     
     
  • 相关阅读:
    C#中对值类型和引用类型的一点认识
    C#随机双色球
    C#中使用ref 和 out 的一点认识
    PHP_零基础学php_3PHP函数、传参函数、默认参数、函数返回值
    PHP_零基础学php_2变量、预定义变量、预定义常量、表达式、运算符、程序控制流程
    PHP_零基础学php
    dedeCMS中单独调用子栏目模板和子栏目的文章时修改源代码给channel和chanenartllist加上limit
    CSS_img标签usemap属性图片中选择区域加入超链接
    C_文件读写流
    怎样从10亿查询词找出出现频率最高的10个
  • 原文地址:https://www.cnblogs.com/changwoo/p/9568476.html
Copyright © 2020-2023  润新知