码迷,mamicode.com
首页 > 其他好文 > 详细

图书管理系统系列之展示,修改,添加,删除

时间:2020-07-21 09:27:21      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:rip   文件   turn   tle   user   render   The   lse   简写   

settings.py配置

      1.当前app的应用名称添加进去
      INSTALLED_APPS = [
            app01, #简写就行
            ]
      2.MIDDLEWARE中把csrf注释掉,防止post请求发送失败
      3.DATABASES = {
            ‘dafault‘:{
                  ‘ENGINE‘:‘django.db.backend.mysql‘,
                  ‘NAME‘:‘booksys‘, #数据库名称
                  ‘HOST‘:‘127.0.0.1‘,#ip
                  ‘PORT‘:3306, #端口
                  ‘USER‘:‘root‘,
                  ‘PASSWORD‘:‘666‘,
                  ‘CHARSET‘:‘utf-8‘,
                  }
            }
      4.静态文件路径的配置
      STATIC_URL  = ‘/static/‘
      STATICFILES_DIRS=[
            os.path.join(BASE_DIR,‘static‘),#里面的static表示的是你自己创的静态文件名称
            ]

models.py文件

      from django.db import models
      class Book(models.Model):
            id = models.AutoField(primarykey=True)#可以不写默认会自动创建
            title = models.CharField(max_length=64,null=True)
            state = models.BooleanField(default=True)
            pub_date = models.DateField(null=True)
            price=models.DecimalField(max_digits=8,decimal_places=2,null=True)
            publish = models.CharField(max_length=32)
            
            def __str__(self):
                  return self.title +‘价格‘+str(self.price)    

url配置

      from django.conf.urls import url
      from django.contrib import admin
      from app01 import views
      urlpatterns = [
            #展示
            url(r‘^book_list‘,views.book_list,name=‘book_list‘),
            #添加
            url(r‘^add_book‘,views.add_book,name=‘add_book‘),
            #修改
            url(r‘^edit_book/(\d+)/‘,views.edit_book,name=‘edit_book‘),
            #删除
            url(r‘^delete_book/(\d+)/‘,views.delete_book,name=‘delete_book‘),
            ]

views视图函数

      from django.shortcuts import render,HttpResponse,redirect
      from app01 import models
      from django.urls import reverse #url别名反向解析,通过name别名对应的数据,解析出我们的url路径
      
      #展示
      def book_list(request):
            books=models.Book.objects.all()
            return render(request,‘book_list.html‘,{‘books‘:books})

      #添加
      def add_book(request):
            if request.method == ‘GET‘:
                  return render(request,‘add_book.html‘)
            else:
                  print(request.POST)#获取post请求过来的数据QueryDict类型
                  title = request.POST.get(‘title‘)
                  price = request.POST.get(‘price‘)
                  pub_date = request.POST.get(‘pub_date‘)
                  publish = request.POST.get(‘publish‘)
                  #操作模型类,添加数据
                  ret = models.Book.objects.create(
                        title=title,
                        price=price,
                        pub_date=pub_date,
                        publish=publish,
                        )
                  return redirect(‘/book_list/‘)#重定向

      #编辑页面
      def edit_book(request,id):
            obj_list = models.Book.objects.filter(pk=id)#pk就相当于数据库中字段id
            if request.method == ‘GET‘:
                  obj = obj_list.first() #获取queryset类型中第一个模型类对象
                  return render(request,‘edit_book.html‘,{‘obj‘:obj})
            else:
                  print(request.POST.dict())#dict()方法能将QueryDict类型数据转换为普通字典类型数据,这边用dict()方法是因为price是decimal类型数据,
                  #print(type(request.POST))#querydict类型
                  obj_list.update(**request.POST.dict())
                  return redirect(‘book_list‘)#通过redirect来进行页面跳转时,redirect方法里面直接写url别名就可以,内部会自动帮我们完成url别名反向解析

      def delete_book(request,id):
            models.Book.objects.filter(pk=id).delete()
            return redirect(‘book_list‘)
  • booklist.html内容
      {% load static %} #加载静态文件
      <!DOCTYPE html>
      <html lang=‘en‘>
      <head>
            <meta charset=‘utf-8‘>
            <title>Title</title>
            <link rel=‘stylesheet‘ href="{% static ‘bootstrap下的css样式路径‘%}">
      </head>
      <body>
            <div class="container">
                  <h1>书籍展示</h1>
                  <div class="col-md-8 col-md-offset-2">
                        <a href={% url ‘add_book‘ %} class=‘btn btn-primary‘>添加书籍</a>
                  <table style="margin-top: 10px;" class="table table-bordered table-striped table-hover">
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>书籍名称</th>
                        <th>价格</th>
                        <th>出版日期</th>
                        <th>出版社</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for book in books %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ book.title }}</td>
                        <td>{{ book.price }}</td>
                        <td>{{ book.pub_date|date:‘Y-m-d‘ }}</td>
                        <td>{{ book.publish }}</td>
                        <td>
                            <a href="{% url ‘edit_book‘ book.id %}" class="btn btn-warning">编辑</a>
                            <a href="{% url ‘delete_book‘  book.pk %}" class="btn btn-danger">删除</a>
                        </td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
      </body>
      </html>
      
  • add_book.html内容
      {% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static ‘plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css‘ %}">
</head>
<body>


<div class="container">

    <h1>添加书籍</h1>
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <form action="/add_book/" method="post">
                <div class="form-group">
                    <label for="title">书籍名称</label>
                    <input type="text" class="form-control" id="title" placeholder="书籍名称" name="title">
                </div>

                <div class="form-group">
                    <label for="price">价格</label>
                    <input type="text" class="form-control" id="price" placeholder="书籍名称" name="price">
                </div>
                <div class="form-group">
                    <label for="pub_date">出版日期</label>
                    <input type="date" class="form-control" id="pub_date" placeholder="书籍名称" name="pub_date">
                </div>
                <div class="form-group">
                    <label for="publish">出版社</label>
                    <input type="text" class="form-control" id="publish" placeholder="书籍名称" name="publish">
                </div>
                <button class="btn btn-success pull-right">提交</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>
  • edit_book.html
      {% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static ‘plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css‘ %}">
</head>
<body>

<div class="container">
    <h1>编辑书籍</h1>
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
{#            <form action="/edit_book/{{ id }}/" method="post">#}
{#            <form action="{% url ‘edit_book‘ id %}" method="post">#}
{#            <form action="{% url ‘edit_book‘ obj.id %}" method="post">#}
{#            <form action="{% url ‘edit_book‘ obj.pk %}" method="post">#}
                <div class="form-group">
                    <label for="title">书籍名称</label>
                    <input type="text" class="form-control" id="title" placeholder="书籍名称" name="title" value="{{ obj.title }}">
                </div>

                <div class="form-group">
                    <label for="price">价格</label>
                    <input type="text" class="form-control" id="price" placeholder="价格" name="price" value="{{ obj.price }}">
                </div>
                <div class="form-group">
                    <label for="pub_date">出版日期</label>
                    <input type="date" class="form-control" id="pub_date" placeholder="出版日期" name="pub_date" value="{{ obj.pub_date|date:‘Y-m-d‘ }}">
                </div>
                <div class="form-group">
                    <label for="publish">出版社</label>
                    <input type="text" class="form-control" id="publish" placeholder="出版社" name="publish" value="{{ obj.publish }}">
                </div>
                <button class="btn btn-success pull-right">提交</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

知识点

      request.POST.dict() #dict()方法可以将QueryDict类型数据转换为普通字典
      obj_list.update(
            **request.POST.dict()  #打散的形式传参 k=v
            )

图书管理系统系列之展示,修改,添加,删除

标签:rip   文件   turn   tle   user   render   The   lse   简写   

原文地址:https://www.cnblogs.com/weiweivip666/p/13352587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!