from django.urls import path from front import views as front_views urlpatterns = [ path('', front_views.index,name='index'), path('add/', front_views.add_book,name='add_book'), path('detail/<int:book_id>/', front_views.book_detail,name='book_detail'), path('del/', front_views.del_book,name='del_book'), ]
from django.shortcuts import render,reverse,redirect from django.db import connection def get_corsor(): return connection.cursor() def index(request): cursor=get_corsor() #cursor.execute("insert into book(id,name,author) values(1,'三國演義','羅貫中')") '''第二使用該界面會錯,這代表每次使用都使用非覆蓋的方式進行填寫''' cursor.execute("select id,name,author from book") books=cursor.fetchall() return render(request,'index.html',context={"books":books}) def del_book(request): if request.method == 'POST': book_id = request.POST.get('book_id') cursor = get_corsor() cursor.execute("delete from book where id=%s" % book_id) return redirect(reverse('index')) else: raise RuntimeError("刪除圖書的method錯誤!") def add_book(request): if request.method == 'GET': return render(request, 'add_book.html') else: name = request.POST.get("name") author = request.POST.get("author") cursor = get_corsor() cursor.execute("insert into book(id,name,author) values(null,'%s','%s')" % (name, author)) return redirect(reverse('index')) def book_detail(request,book_id): cursor=get_corsor() cursor.execute("select id,name,author from book where id=%s" % book_id) book=cursor.fetchone()#使用fetchone將會填充不了 return render(request, 'book_detail.html', context={"book":book})
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'book_managr', 'USER':'root', 'PASSWORD':'', 'HOST':'127.0.0.1', 'PORT':'3306', } }
{% extends 'base.html' %} {% block content %} <p>書名:{{ book.1}}</p> <p>做者:{{ book.2 }}</p> <form action="{% url 'del_book' %}" method="post"> <input type="hidden" name="book_id" value="{{ book.0 }}"> <input type="submit" value="刪除按鈕"> </form> {% endblock %}
{% extends 'base.html' %} {% block content %} <form action="" method="post"> <table> <tbody> <tr> <td>書名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>做者:</td> <td><input type="text" name="author"></td> </tr> <tr> <td></td> <td><input type="submit" value="提交"></td> </tr> </tbody> </table> </form> {% endblock %} index {% extends 'base.html' %} {% block content %} <table> <thead> <tr> <th>序號</th> <th>書名</th> <th>做者</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td><a href="{% url 'book_detail' book_id=book.0 %}">{{ book.1 }}</a></td> <td>{{ book.2 }}</td> </tr> {% endfor %} </tbody> </table> {% endblock %}
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖書管理系統</title> <link rel="stylesheet" href="{% static 'front/base.css' %}"> </head> <body> <nav> <ul class="nav"> <li><a href="/">首頁</a></li> <li><a href="{% url 'add_book' %}">發佈圖書</a></li> </ul> </nav> {% block content %}{% endblock %} </body> </html>
*{ margin: 0; padding: 0; } .nav{ background: #3a3a3a; height: 65px; overflow: hidden; } .nav li{ float: left; list-style: none; margin: 0 20px; line-height: 65px; } .nav li a{ color: #fff; text-decoration: none; } .nav li a:hover{ color: lightblue; }