基于django图书信息管理系统的搭建(增删改查)

news/2025/2/26 7:52:32

django项目搭建教程 ☞      ----------------- 教程    

 本文主要讲解django如何连接数据库MySQL并且可视化展示,实现增删改查功能


目录

django%E5%BA%94%E7%94%A8-toc" name="tableOfContents" style="margin-left:0px">一. 创建django应用

二. 数据库配置

三. 查看数据库 

四. 编写代码

4.1视图函数 

4.2 配置URL

4.3创建模板文件 

4.3.1 book_list.html 

4.3.2 book_create.html 

4.3.3 book_update.html 

五. 系统展示 

六. 更多干货


django%E5%BA%94%E7%94%A8" name="%E4%B8%80.%20%E5%88%9B%E5%BB%BAdjango%E5%BA%94%E7%94%A8">一. 创建django应用

在创建完项目的基础上创建应用,点击文章首部“教程” 0基础也能搭建好项目

python">python manage.py startapp myapp ---应用名为myapp,可自行修改

创建完应用在项目文件夹下的settings.py中的INSTALLED_APPS 添加一行 "myapp"

项目文件夹下会出现 myapp 文件夹,定义模型 在 myapp/models.py 文件中定义 Book 模型

python">from django.db import models

class Book(models.Model): ------表结构
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    publication_year = models.IntegerField()

    def __str__(self):
        return self.title

二. 数据库配置

 在项目文件夹下的settings.py添加

python">DATABASES = {
    # "default": {
    #     "ENGINE": "django.db.backends.sqlite3",
    #     "NAME": BASE_DIR / "db.sqlite3",
    # }
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'date',----数据库名字
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }

}

 注意:配置完成后,在MySQL执行CREATE DATABASE date 命令创建数据库

 迁移数据库:执行以下命令来创建数据库表

python">python manage.py makemigrations
python manage.py migrate

和下图一致表明成功了 

 

三. 查看数据库 

 我们可以看到自动生成了如下表,其中最下面的表就是我们所定义的book模型

四. 编写代码

4.1视图函数 

在 myapp/views.py 文件中编写视图函数

python">from django.shortcuts import render, redirect
from.models import Book

def book_list(request):
    books = Book.objects.all()
    return render(request, 'book_list.html', {'books': books})

def book_create(request):
    if request.method == 'POST':
        title = request.POST['title']
        author = request.POST['author']
        publication_year = request.POST['publication_year']
        Book.objects.create(title=title, author=author, publication_year=publication_year)
        return redirect('book_list')
    return render(request, 'book_create.html')

def book_update(request, pk):
    book = Book.objects.get(pk=pk)
    if request.method == 'POST':
        book.title = request.POST['title']
        book.author = request.POST['author']
        book.publication_year = request.POST['publication_year']
        book.save()
        return redirect('book_list')
    return render(request, 'book_update.html', {'book': book})

def book_delete(request, pk):
    book = Book.objects.get(pk=pk)
    book.delete()
    return redirect('book_list')

4.2 配置URL

在 项目文件夹下的urls.py 文件中配置URL

python">from django.contrib import admin
from django.urls import path
from myapp.views import book_list, book_create, book_update, book_delete

urlpatterns = [
    path('admin/', admin.site.urls),
    path('books/', book_list, name='book_list'),
    path('books/create/', book_create, name='book_create'),
    path('', book_update, name='book_update'),
    path('', book_delete, name='book_delete'),
]

4.3创建模板文件 

 在 myapp/templates 目录下创建以下HTML文件,没有目录则先创建目录

4.3.1 book_list.html 

<!DOCTYPE html>
<html>
<head>
    <title>Book List</title>
</head>
<body>
    <h1>Book List</h1>
    Create New Book
    <ul>
        {% for book in books %}
        <li>{{ book.title }} - {{ book.author }} ({{ book.publication_year }})
            Update
            Delete
        </li>
        {% endfor %}
    </ul>
</body>
</html>

4.3.2 book_create.html 

<!DOCTYPE html>
<html>
<head>
    <title>Create Book</title>
</head>
<body>
    <h1>Create New Book</h1>
    <form method="post">
        {% csrf_token %}
        <label for="title">Title:</label><br>
        <input type="text" id="title" name="title" required><br>
        <label for="author">Author:</label><br>
        <input type="text" id="author" name="author" required><br>
        <label for="publication_year">Publication Year:</label><br>
        <input type="number" id="publication_year" name="publication_year" required><br>
        <input type="submit" value="Create">
    </form>
</body>
</html>

4.3.3 book_update.html 

<!DOCTYPE html>
<html>
<head>
    <title>Update Book</title>
</head>
<body>
    <h1>Update Book</h1>
    <form method="post">
        {% csrf_token %}
        <label for="title">Title:</label><br>
        <input type="text" id="title" name="title" value="{{ book.title }}" required><br>
        <label for="author">Author:</label><br>
        <input type="text" id="author" name="author" value="{{ book.author }}" required><br>
        <label for="publication_year">Publication Year:</label><br>
        <input type="number" id="publication_year" name="publication_year" value="{{ book.publication_year }}" required><br>
        <input type="submit" value="Update">
    </form>
</body>
</html>

五. 系统展示 

 

 

六. 更多干货

------------   --  ✈✈✈
1.如果我的博客对你有帮助或你喜欢我的博客内容,请 “👍点赞” “✍️评论” “收藏” 一键三连哦!

2.❤️【👇🏻👇🏻👇🏻关注我| 获取更多源码 | 优质文章】 带您学习各种前端插件、3D炫酷效果、图片展示、文字效果、以及整站模板 、HTML模板 、微信小程序模板 、等! 「在这里一起探讨知识,互相学习」!

3.以上内容技术相关问题✉欢迎一起交流学习 ☟   ☟   ☟


http://www.niftyadmin.cn/n/5868338.html

相关文章

vue2 ruoyi websocket轮询

文章目录 前言一、websocket和心跳是什么&#xff1f;二、使用步骤1.2.监听变化3.关闭 总结 前言 websocket&#xff0c;实现与后端通讯&#xff0c;使用心跳机制&#xff0c;断联自动恢复。 一、websocket和心跳是什么&#xff1f; WebSocket WebSocket 是一种网络通信协议&a…

JSP+Servlet 实现分页(Tomcat 9)

访问地址 项目路径/mulPageSystem java文件&#xff1b;包名org.rain.bean 文件名 PageSystemBean package org.rain.bean; public class PageSystemBean { private String school; private String home; public String getSchool() { return school; } public void s…

Vue的项目创建以及项目目录与组合式API

一.创建Vue 1.Vue-CLI:创建Vue的脚手架工具 2.Create-vue&#xff1a;是Vue官方提供的脚手架之一,底层采用官方自主研发的vite,快捷&#xff0c;开发方便。 3.准备工作:系统中需要安装nodejs环境&#xff0c;在该环境中提供npm包管理器 4.创建Vue项目的命令:npm init vuela…

最新前端框架选型对比与建议(React/Vue/Svelte/Angular)

前端框架选型对比与建议&#xff08;React/Vue/Svelte/Angular&#xff09; 一、核心框架技术特性对比&#xff08;基于最新版本&#xff09; 维度React 19 25Vue 3.5 12Svelte 5 25Angular 19 5核心理念函数式编程、JSX语法、虚拟DOM渐进式框架、组合式API、模板语法编译时框…

SQL注入(order by,limit),seacms的报错注入以及系统库的绕过

1&#xff1a;如果information_schema被过滤了&#xff0c;该怎么绕过 1.1&#xff1a;介绍一下information_schema这个库 information_schema 是一个非常重要的系统数据库&#xff0c;它在SQL标准中定义&#xff0c;并且被许多关系型数据库管理系统&#xff08;RDBMS&#x…

音视频编码和封装格式

文章目录 音视频编码格式音频编码视频编码 音视频封装格式 音视频编码格式 音频编码 音频编码是一种将音频信号转换为数字形式的过程&#xff0c;目的是减少数据中的冗余&#xff0c;以便存储和传输。 音频编码的实质是通过抽样、量化和编码三个步骤&#xff0c;将连续变化的…

爬虫运行后如何保存数据?

爬虫运行后&#xff0c;将获取到的数据保存到本地或数据库中是常见的需求。Python 提供了多种方式来保存数据&#xff0c;包括保存为文本文件、CSV 文件、JSON 文件&#xff0c;甚至存储到数据库中。以下是几种常见的数据保存方法&#xff0c;以及对应的代码示例。 1. 保存为文…

class绑定和style绑定

class绑定 简单对象绑定 <template> <div><p :class"{active:isActive,text-danger:hasError}">Class样式绑定1</p> </div> </template> <script> export default{data(){return{isActive:true,hasError:true,}} } <…