您的位置:首页 > 汽车 > 新车 > 湖南岳阳最新疫情_动漫制作与设计专业_网络推广平台软件_广州seo团队

湖南岳阳最新疫情_动漫制作与设计专业_网络推广平台软件_广州seo团队

2025/3/16 20:29:36 来源:https://blog.csdn.net/m0_63057469/article/details/145853725  浏览:    关键词:湖南岳阳最新疫情_动漫制作与设计专业_网络推广平台软件_广州seo团队
湖南岳阳最新疫情_动漫制作与设计专业_网络推广平台软件_广州seo团队

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

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


目录

一. 创建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应用

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

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

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

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

from django.db import modelsclass 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添加

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 manage.py makemigrations
python manage.py migrate

和下图一致表明成功了 

 

三. 查看数据库 

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

四. 编写代码

4.1视图函数 

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

from django.shortcuts import render, redirect
from.models import Bookdef 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

from django.contrib import admin
from django.urls import path
from myapp.views import book_list, book_create, book_update, book_deleteurlpatterns = [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 }})UpdateDelete</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.以上内容技术相关问题✉欢迎一起交流学习 ☟   ☟   ☟

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com