zoukankan      html  css  js  c++  java
  • 图书管理系统(三):作者列表增加、删除和编辑

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

    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>
     
     
     
  • 相关阅读:
    html页面怎样能够自适应电脑屏幕宽度
    js语法大全
    DevExtreme- jQuery 表格组件-DataGrid
    DevExtreme 图表组件
    C# 中的委托和事件
    SQL Server游标的使用
    Oracle11g使用exp导出空表
    PowerDesigner设计Name和Comment 替换
    Toad for Oracle 快捷键
    Windows 命令大全
  • 原文地址:https://www.cnblogs.com/changwoo/p/9568476.html
Copyright © 2011-2022 走看看