05-图书管理系统-功能开发

d:\python\demo2

目标

  • 页面开发
  • views开发
  • urls映射

页面开发

bookapp/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Neuedu</title>
</head>
<body>
<h1>欢迎来到图书管理系统</h1>
<button onclick="init()">添加一本图书信息</button>
<button onclick="search()">查询图书信息</button>
</body>
<script>
    function init(){
        window.location.href = "//www.greatytc.com/books/init";
    }

    function search(){
        window.location.href = "//www.greatytc.com/books/search";
    }
</script>
</html>

bookapp/templates/addbook.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Neuedu</title>
</head>
<body>
<h1>图书添加</h1>
添加成功!
<a href="//www.greatytc.com/books">返回</a>
</body>
</html>

bookapp/templates/showbooks.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Neuedu</title>
</head>
<body>
<h1>图书信息</h1>
<table border="1">
    <tr>
        <th>序号</th><th>书名</th><th>作者</th><th>出版社</th><th>建议零售价</th>
    </tr>
    {% for book in books %}
    <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ book.bookname }}</td>
        <td>{{ book.author }}</td>
        <td>{{ book.publisher }}</td>
        <td>{{ book.price }}</td>
    </tr>
    {% endfor %}
</table>
<a href="//www.greatytc.com/books">返回</a>
</body>
</html>

bookapp/views开发

from django.shortcuts import render
from bookapp.models import Book


# Create your views here.
def index(request):
    return render(request, "index.html")


def init(request):
    mybook = Book(bookname='mybook', author='myself', publisher='Neuedu', price=20)
    mybook.save()
    return render(request, "addBook.html")


def search(request):
    context = {}
    context['books'] = Book.objects.all()
    return render(request, 'showBooks.html', context)

urls映射

urls

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('books/', include('bookapp.urls')),
]

bookapp/urls

from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
    path('init', views.init, name='init'),
    path('search', views.search, name='search'),
]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容