介绍:
本程序仅仅实现图书数据的增删查
树形结构如下
 
全部代码如下:
url:
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'),
]
 

 

app的views;
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})

 

 
setting:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'book_managr',
'USER':'root',
'PASSWORD':'',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}

 

 
html:book_detail
{% 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 %}

  

 
add
{% 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 %}
 

  

base:
{% 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>

 

css:base:
 
    
*{
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;
}
 

 

posted on 2019-09-16 16:45  topass123  阅读(664)  评论(0编辑  收藏  举报