首页 > 其他 > 详细

django 项目之图书管理系统

时间:2021-04-29 22:23:18      阅读:19      评论:0      收藏:0      [点我收藏+]

一:图书管理首页展示

  • 1.设计数据库字段
在models.py文件中设计表字段

from django.db import models

# Create your models here.
from django.db import models

# Create your models here.


class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=8,decimal_places=2)
publish_date = models.DateField(auto_now_add=True)
# 一对多
publish = models.ForeignKey(to=‘Publish‘)
# 多对多
authors = models.ManyToManyField(to=‘Author‘)

def __str__(self):
    return self.title


class Publish(models.Model):
name = models.CharField(max_length=32)
addr = models.CharField(max_length=64)
email = models.EmailField()
# varchar(254)  该字段类型不是给models看的 而是给后面我们会学到的校验性组件看的

def __str__(self):
    return ‘对象:%s‘%self.name


class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
# 一对一
author_detail = models.OneToOneField(to=‘AuthorDetail‘)


class AuthorDetail(models.Model):
phone = models.BigIntegerField()  # 电话号码用BigIntegerField或者直接用CharField
addr = models.CharField(max_length=64)
  • 2进行数据库的更改
在seetings配置文件中
	DATABASES = {
	‘default‘: {
    ‘ENGINE‘: ‘django.db.backends.mysql‘,
    ‘NAME‘: ‘day65‘,
    ‘HOST‘:‘127.0.0.1‘,
    ‘PORT‘:3306,
    ‘USER‘:‘root‘,
    ‘PASSWORD‘:‘admin123‘,
    ‘CHARSET‘:‘utf8‘
}
}

在随意的__init__ 文件下
import pymysql
pymysql.install_as_MySQLdb()


执行数据库的迁移指令
python manage.py makemigrations

python manage.py migrate
  • 3.首页的搭建

url.py 文件

from app01 import views

url(r‘^$‘,views.home,home=‘home‘)

views.py 文件

def home(request)
	return render(request,‘home.html‘)

在templates文件夹下新建html文件

该html文件就是对应的前端页面
  • 4图书列表的展示代码

url.py 路由部分
url(r‘^book/list/‘,views.book_list,name=‘book_list‘)

图书的展示代码 views.py

from app01 import models
def book_list(request):
	#先查询出所有的数据信息 传递给html页面
	book_queryset=models.Book.objects.all()
	return render(request,‘book_list‘,locals())

对应的前端代码如下

"""
{% extends ‘home.html‘ %}
{% block content %}
<a href="{% url ‘book_add‘ %}" class="btn btn-success">添加</a>
<br>
<br>
<table class="table table-hover table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>书名</th>
            <th>价格</th>
            <th>出版日期</th>
            <th>出版社</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        {% for book_obj in book_queryset %}
            <tr>

                <td>{{ book_obj.pk }}</td>
                <td>{{ book_obj.title }}</td>
                <td>{{ book_obj.price }}</td>
                <td>{{ book_obj.publish_date|date:‘Y-m-d‘ }}</td>
                <td>{{ book_obj.publish.name }}</td>
                <td>
                    {% for author_obj in book_obj.authors.all %}
                        {% if forloop.last %}
                            {{ author_obj.name }}
                        {% else %}
                            {{ author_obj.name }}、
                        {% endif %}
                    {% endfor %}
                </td>
                <td>
                    <a href="{% url ‘book_edit‘ book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a>
                    <a href="{% url ‘book_delete‘ book_obj.pk %}" class="btn btn-danger btn-xs">删除</a>
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
"""

二:书籍的添加

urls.py

url(r‘^book/add/‘,views,book_add,name=‘book_add‘)

views.py

def book_add(request):
	return render(request,‘book_add.html‘)

先把settings配置文件中的MIDDLEWARE 中的验证文件的注掉

在views.py

if request.method == ‘POST‘:
	#获取前端提交过来的所有数据
	title = request.POST.get(‘titile‘)
	price = request.POST.get(‘price‘)
	publish_date = request.POST.get(‘publish_date‘)
	publish_id = request.POST.get(‘publish‘)
	authors_list = request.POST.get(‘authors‘)
	
	#操作数据库存储数据
	# 书籍表
    book_obj = models.Book.objects.create(title=title,price=price,publish_date=publish_date,publish_id=publish_id)
    # 书籍与作者的关系表
    book_obj.authors.add(*authors_list)
    # 跳转到书籍的展示页面
    """
    redirect括号内可以直接写url
    其实也可以直接写别名
    
    但是如果你的别名需要额外给参数的话,那么就必须使用reverse解析了
    """
    return redirect(‘book_list‘)

先获取当前系统中所有出版社信息和作者信息
publish_queryset =models.Publish.objects.all()
author_queryset=models.Author.objects.all()
return render(request,‘book_add.html‘,locals())

book_add.html

{% extends ‘home.html‘ %}
{% block content %}
<h1 class="text-center">书籍添加</h1>
<form action="" method="post">
    <p>书名:
        <input type="text" name="title" class="form-control">
    </p>
    <p>价格:
        <input type="text" name="price" class="form-control">
    </p>
    <p>出版日期:
        <input type="date" name="publish_date" class="form-control">
    </p>
    <p>出版社:
        <select name="publish" id="" class="form-control">
            {% for publish_obj in publish_queryset %}
                <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
            {% endfor %}
        </select>
    </p>
    <p>作者:
        <select name="authors" id="" multiple class="form-control">
            {% for author_obj in author_queryset %}
                <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
            {% endfor %}
        </select>
    </p>
    <input type="submit" value="新增" class="btn btn-primary btn-block">
</form>
{% endblock %}

三:书籍的编辑

url.py

url(r‘book/edit/(?P<edit_id>\d+)‘,views.book_edit,name="book_edit")

views.py

def book_edit(request,edit_id):
# 获取当前用户想要编辑的书籍对象 展示给用户看
edit_obj = models.Book.objects.filter(pk=edit_id).first()
if request.method == ‘POST‘:
    title = request.POST.get("title")
    price = request.POST.get("price")
    publish_date = request.POST.get("publish_date")
    publish_id = request.POST.get("publish")
    authors_list = request.POST.getlist("authors")  # [1,2,3,4,]
    models.Book.objects.filter(pk=edit_id).update(title=title,
                                                  price=price,
                                                  publish_date=publish_date,
                                                  publish_id=publish_id
                                                  )
    # 该第三张关系表
    edit_obj.authors.set(authors_list)
    return redirect(‘book_list‘)

publish_queryset = models.Publish.objects.all()
author_queryset = models.Author.objects.all()
return render(request,‘book_edit.html‘,locals())

然后就是对应的html

{% extends ‘home.html‘ %}


{% block content %}
<h1 class="text-center">书籍编辑</h1>
<form action="" method="post">
    <p>书名:
        <input type="text" name="title" class="form-control" value="{{ edit_obj.title }}">
    </p>
    <p>价格:
        <input type="text" name="price" class="form-control" value="{{ edit_obj.price }}">
    </p>
    <p>出版日期:
        <input type="date" name="publish_date" class="form-control" value="{{ edit_obj.publish_date|date:‘Y-m-d‘ }}">
    </p>
    <p>出版社:
        <select name="publish" id="" class="form-control">
            {% for publish_obj in publish_queryset %}
{#                    针对当前书籍对象的出版社应该默认选中#}
                {% if edit_obj.publish == publish_obj %}
                    <option value="{{ publish_obj.pk }}" selected>{{ publish_obj.name }}</option>
                {% else %}
                    <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
                {% endif %}

            {% endfor %}
        </select>
    </p>
    <p>作者:
        <select name="authors" id="" multiple class="form-control">
            {% for author_obj in author_queryset %}
                {% if author_obj in edit_obj.authors.all %}
                    <option value="{{ author_obj.pk }}" selected>{{ author_obj.name }}</option>
                {% else %}
                    <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
                {% endif %}

            {% endfor %}
        </select>
    </p>
    <input type="submit" value="编辑" class="btn btn-info btn-block">
</form>
{% endblock %}

四:书籍的删除

url.py

url(r‘^book/delete/(\d+)/‘,views.book_delete,name="book_delete")

views.py

def book_delete(request,delete_id):
# 简单粗暴 直接删除
models.Book.objects.filter(pk=delete_id).delete()
# 直接跳转到展示页
return redirect(‘book_list‘)

django 项目之图书管理系统

原文:https://www.cnblogs.com/0jiaqing0/p/14719489.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!