• 添加书籍:


    views:

    #定义新增书功能:
    def add_book(request):
    if request.method == "POST":
    book_name = request.POST.get("book_name")
    pub_id = request.POST.get("pub_id")
    #插入数据库:
    # models.Book.objects.create(name=book_name,pub=models.Publisher.objects.get(pk=pub_id))
    obj = models.Book.objects.create(name=book_name,pub_id=pub_id)
    return redirect("/book/")
    #查询所有的出版社:
    add_pubs = models.Publisher.objects.all()
    return render(request,"add_book.html",{"add_pubs":add_pubs})
    add_html:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <form action="" method="post">
    <p>
    书名:<input type="text" name="book_name">
    </p>

    <p>
    出版社:
    <select name="pub_id" id="">
    {% for pub in add_pubs %}
    <option value="{{ pub.pk }}">{{ pub.name }}</option>
    {% endfor %}

    </select>
    </p>
    <button>提交</button>
    </form>
    </body>
    </html>
    展示书views:
    #定义展示书功能:
    def book(request):
    #查询所有书籍:
    all_books = models.Book.objects.all()
    # for book in all_books:
    # print(book)
    # print(book.id)
    # print(book.pk)
    # print(book.pub_id) #所关联的出版社对象的id 对象.外键_id
    # print(book.pub) #所关联的出版社对象 对象.外键
    return render(request,"book.html",{"all_books":all_books})
    展示书HTML:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <table border="1">
    <thead>
    <tr>
    <th>序号</th>
    <th>id</th>
    <th>书名</th>
    <th>出版社名字</th>
    <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for book in all_books %}
    <tr>
    <td>{{ forloop.counter }}</td>
    <td>{{ book.pk }}</td>
    <td>{{ book.name }}</td>
    <td>{{ book.pub }}</td>
    <td></td>
    </tr>
    {% endfor %}

    </tbody>
    </table>
    </body>
    </html>
  • 相关阅读:
    Object-C中
    实例变量可见度修饰符
    Object-C 类和对象
    C语言中线程和进程的区别
    动态内存分配
    C语言中union关键字
    C语言结构体
    const define static extern 关键词详解
    基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(一)
    Ubuntu18.04+CUDA9.0+cuDNN7.1.3+TensorFlow1.8 安装总结
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12039072.html
Copyright © 2020-2023  润新知