zoukankan      html  css  js  c++  java
  • 图书管理系统系列之展示,修改,添加,删除

    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(primary_key=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
                )
    

    -------------------------------------------

    个性签名:代码过万,键盘敲烂!!!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    jq工具函数(八)使用$.extend()扩展工具函数
    jq工具函数(七)URL操作函数
    jq工具函数(六)字符串操作函数
    jq工具函数(四)检测对象是否为原始对象
    jq工具函数(二)检测浏览器是否属于W3C盒子模型
    jq工具类函数(一)获取浏览器的名称与版本信息
    linux
    记录---待探索
    html倒计时 照着练习(抄袭)的
    css基础
  • 原文地址:https://www.cnblogs.com/weiweivip666/p/13352587.html
Copyright © 2011-2022 走看看