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


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

    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>
     
     
     
  • 相关阅读:
    python 默认编码( UnicodeDecodeError: 'ascii' codec can't decode)
    python发送各类邮件的主要方法
    python输出htmltestrunner中文乱码如何解决
    Python unittest 官方文档
    Python pip 安装包
    Python easy_insatll 安装包
    linux 解压操作命令
    vim 操作指令2
    vim 操作指令1
    (转)水波纹过渡特效
  • 原文地址:https://www.cnblogs.com/changwoo/p/9568476.html
Copyright © 2020-2023  润新知