wtf

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

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

    # 展示图书信息
    url(r'^booklist/', views.booklist),
    # 添加图书信息
    url(r'^add_book/', views.add_book),
    # 编辑图书信息
    url(r'^edit_book/', views.edit_book),
    # 删除图书信息
    url(r'^delete_book/', views.delete_book),


]

#views.py
def booklist(request):
    book_list=models.Book.objects.all()
    return render(request,'book.html',{'book_list':book_list})

def add_book(request):
   
    if request.method=='POST':
        bookname=request.POST.get('bookname')
        price=request.POST.get('price')
        press=request.POST.get('press')
        author=request.POST.get('author')
        #直接写入数据库
     
        book_obj=models.Book.objects.create(bookname=bookname,price=price,press=press,author=author)
        
        return redirect('/booklist')
    return render(request,'add_book.html')

def edit_book(request):
   
    edit_id = request.GET.get('edit_id')
    if request.method=='POST':
        bookname = request.POST.get('bookname')
        price = request.POST.get('price')
        press = request.POST.get('press')
        author = request.POST.get('author')

     
        models.Book.objects.filter(id=edit_id).update(bookname=bookname,price=price,press=press,author=author)
        '''
        批量操作 会将filter查询出来的列表中所有对象全部更新
        '''
        return redirect('/booklist')
   
    edit_obj = models.Book.objects.filter(id=edit_id).first() #不推荐使用索引取值


    return render(request,'edit_book.html',{'edit_obj':edit_obj})

def delete_book(request):
    #从request.GET中获取到用户想要删除的数据id值
    delete_id=request.GET.get('delete_id')
    #直接删除数据
    models.Book.objects.filter(id=delete_id).delete()
    '''
    批量操作 会将filter查询出来的列表中的所有对象全部删除
    '''
    return redirect('/booklist/')
#book.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
</head>
<body>
<div class="container">
    <div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1 class="text-center">图书表</h1>
        <a href="/add_book/" class="btn btn-success">新增</a>
        <table class="table table-hover table-bordered table-striped">
            <thead>
                <tr>
                    <th>id</th>
                    <th>bookname</th>
                    <th>price</th>
                    <th>press</th>
                    <th>author</th>
                    <th>action</th>
                </tr>
            </thead>
            <tbody>
{#                {{ user_list }}:[user_obj,user_obj1,user_obj2]#}
                {% for book in book_list %}
                    <tr>
                      <td>{{ book.id }}</td>
                      <td>{{ book.bookname }}</td>
                      <td>{{ book.price }}</td>
                      <td>{{ book.press }}</td>
                      <td>{{ book.author }}</td>
                      <td>
                           <a href="/edit_book/?edit_id={{ book.id }}" class="btn btn-primary btn-sm">编辑</a>
                           <a href="/delete_book/?delete_id={{ book.id }}" class="btn btn-danger btn-sm">删除</a>
                      </td>
                    </tr>
                {% endfor %}

            </tbody>
        </table>
    </div>
    </div>
</div>
</body>
</html>
#models.py
class Book(models.Model):
    bookname=models.CharField(max_length=255)
    price=models.DecimalField(max_digits=8,decimal_places=2)
    press=models.CharField(max_length=32)
    author=models.CharField(max_length=32)
posted on 2019-11-24 22:57  wtfss  阅读(111)  评论(0)    收藏  举报