作者的查,增,删,改

阅读目录

APP中的models

# 作者
class Author(models.Model):
    name = models.CharField(max_length=32, unique=True)
    books = models.ManyToManyField('Book',through='Author_book')    

    def __str__(self):
        return self.name


class Author_book(models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE)
    book = models.ForeignKey('Book', on_delete=models.CASCADE)
    date = models.DateTimeField()

urls 

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^publisher/', views.publisher),
    url(r'^add_publisher/', views.add_publisher),
    url(r'^del_publisher/', views.del_publisher),
    url(r'^edit_publisher/', views.edit_publisher),

    url(r'^test/', views.test),

    # 展示作者
    url(r'^author/', views.author),

    # 增加作者
    url(r'^add_author/', views.add_author),

    # 删除作者
    url(r'^del_author/', views.del_author),

    # 编辑作者
    url(r'^edit_author/', views.edit_author),

]

App中的views 与  templates

查操作

views

 

def author(request):
    # 查询所有作者
    all_authors = models.Author.objects.all().order_by('pk')     #按照作者的ID进行排序

    for author in all_authors:  
        print(author)                  #所有的作者对象
        print(author.pk)            
        print(author.name)
        print(author.books, type(author.books))  # 多对多关系管理对象
        print(author.books.all(), type(author.books.all()))  # 多对多关系管理对象对的book对象
        print('*' * 30)
    return render(request, 'author.html', {'all_authors': all_authors})

 

 

templates

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>序号</th>
        <th>姓名</th>
        <th>代表作</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for author in all_authors %}
        <tr>
            <td>{{ forloop.counter }}</td>        #序号
            <td>{{ author.name }}</td>        
            <td>
                {% for book in  author.books.all %}   #通过多对多管理对象得到每个book对象
                    {% if forloop.last %}              #如果循环到最后一个
                        《{{ book.name }}》  
                    {% else %}
                        《{{ book.name }}》、
                    {% endif %}
                {% endfor %}
            </td>
            <td>
                <a href="/del_author/?pk={{ author.pk }}">删除</a>
                <a href="/edit_author/?pk={{ author.pk }}">编辑</a>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>

 

 

增操作

 

views

# 增加作者
def add_author(request):
    if request.method == 'POST':
        # 新增数据
        # 获取提交的数据
        name = request.POST.get('name')
        books_id = request.POST.getlist('books_id')     #获得的是一个列表

        # 插入到数据库中
        author_obj = models.Author.objects.create(name=name)
       # 绑定书籍和作者的关系
        author_obj.books.set(books_id)   

        # 跳转到展示页面
        return redirect('/author/')

    # 查询所有的书籍
    all_books = models.Book.objects.all()

    return render(request, 'add_author.html', {'all_books': all_books})

 

templates

<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>

<form action="" method="post">
    <p>
        姓名:<input type="text" name="name">
    </p>
    <p>
        代表作:
        <select name="books_id" id="" multiple>
            {% for book in all_books %}
                <option value="{{ book.pk }}">{{ book.name }}</option>
            {% endfor %}
        </select>
    </p>
    <button>提交</button>
</form>
</body>
</html>

删操作

views

def del_author(request):
    pk = request.GET.get('pk')
    models.Author.objects.filter(pk=pk).delete()
    # 1.删除作者的书籍
    # 2.删除作者和书籍的多对多的关系
    return redirect('/author/')

 

 

改操作

views

def edit_author(request):
    # 查询作者
    pk = request.GET.get('pk')
    author_obj = models.Author.objects.get(pk=pk)

    books_id = [ i.book_id for i in models.Author_book.objects.filter(author_id=pk) ]
    books = models.Book.objects.filter(pk__in=books_id)

    if request.method == 'POST':
        # 修改数据
        new_name = request.POST.get('name')
        books_id = request.POST.getlist('books_id')
        # 修改作者姓名
        author_obj.name = new_name
        author_obj.save()
        # 修改作者和书籍多对多的关系
        author_obj.books.set(books_id)

        return redirect('/author/')

    # 查询所有的书籍
    all_books = models.Book.objects.all()

    return render(request, 'edit_author.html', {'author_obj': author_obj, 'all_books': all_books,'books':books})

templates

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<form action="" method="post">
    <p>
        姓名:<input type="text" name="name" value="{{ author_obj.name }}">
    </p>
    <p>
        代表作:
        <select name="books_id" id="" multiple>
            {% for book in all_books %}
                {% if book in books %}
                    <option selected value="{{ book.pk }}">{{ book.name }}</option>
                {% else %}
                    <option value="{{ book.pk }}">{{ book.name }}</option>
                {% endif %}
            {% endfor %}
        </select>
    </p>
    <button>提交</button>
</form>
</body>
</html>

 

posted @ 2019-02-13 01:23  小萍瓶盖儿  阅读(155)  评论(0编辑  收藏  举报