• 添加书籍:


    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>
  • 相关阅读:
    Android OpenGL(2)
    Bootloader:BareBox
    Android OpenGL(1)
    Android脚本环境
    Android用户界面开发:事件处理
    S5PV210按键控制LED
    S5PV210控制蜂鸣器
    Windows Vista/Windows 7上安装wince5.0/6.0及SDK模拟器
    Makefile
    每日英语:Apps Reorder the Job Landscape
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12039072.html
Copyright © 2020-2023  润新知