当前位置:网站首页>ORM学员管理系统
ORM学员管理系统
2022-07-21 11:50:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
1.使用MySQL自己创建一个数据库,以下例为例
CREATE DATABASE orm DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
2.在项目的settings.py文件中进行数据库链接信息
# 修改django默认的数据库的sqlite3为mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #通过这个去链接mysql
'NAME': 'djangotsgl',
'USER':'root',
'PASSWORD':'123456',
'HOST':'localhost',
'PORT':'3306',
}
}
3.在项目下的__init__文件中写入以下命令
import pymysql
pymysql.install_as_MySQLdb()
4.在项目下的models.py文件中创建表数据
models.py
class Book(models.Model): #必须要继承的
nid = models.AutoField(primary_key=True) #自增id(可以不写,默认会有自增id)
title = models.CharField(max_length=32)
publishDdata = models.DateField() #出版日期
author = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5,decimal_places=2) #一共5位,保留两位小数
5.进行数据库迁移并形成相关数据
python manage.py makemigrations 记录models.py中的改动,具体的记录在案
python manage.py migrate 把相关的改动翻译成SQL语句并执行
6.在url文件中进行相关的路径配置
先从APP中导入与路径相匹配的视图函数
from app01 import views
将路径和试图函数的对应关系配置好
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^classes_list/', views.classes_list),
]
进行相关的业务逻辑处理
APP/views.py
先导入相关所需模块儿
from django.shortcuts import render,redirect,HttpResponse
from .models import Classes
进行相关业务逻辑处理
def classes(request):
data = Classes.objects.all() # 获取数据库中的相关数据
return render(request,'classes_list.html',{'data':data}) #将数据展示到页面
在templetes文件中进行页面渲染
class_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>班级表</title>
</head>
<body>
<a href="#">添加班级</a>
<table border="1">
<thead>
<th>ID</th>
<th>班级名称</th>
<th>操作</th>
</thead>
<tbody>
{% for class in data %}
<tr>
<td>{{ class.id }}</td>
<td>{{ class.name }}</td>
<td>
<a href="#">删除</a>
</td>
<td>
<a href="#">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124452.html原文链接:https://javaforall.cn
边栏推荐
- 第三方之百度AI使用总结
- Reentrant read write lock feature summary of reentrantreadwritelock
- 不得不了解的四大作用域:application,session,request,page
- [零基础] 减少50%的bug,只需要30分钟...
- 机器学习可解释性
- 快速判断一个文件是否有病毒
- 16.target和currenTarget的区别?
- easyui框架dialog重复加载的问题
- Implementing DDD based on ABP -- domain service, application service and dto practice
- 移动端基础
猜你喜欢
Interview must ask: from entering URL to page display, what happened? (detailed and easy to understand, organized and easy to remember)
Mobile end Foundation
[zero base] reduce bugs by 50% in only 30 minutes
Test: Generic Cabling
DDD - Domain service, Application Service and dto Practice Based on ABP
inoic4学习笔记2
Judge whether to balance binary tree
Summary of best practices for openfegin/restemplate microservice invocation
[零基础] 减少50%的bug,只需要30分钟...
js中的转义字符?
随机推荐
下载工具-谷歌插件 tampermonkey 和 greasyfork
When uploading jars on the nexus management page, jars can be pulled to the project normally. Jars published using the deploy of idea lifecycle can only be pulled to POM. 401 problem
What does the residual network solve and why is it effective abstract
Binary search tree and bidirectional linked list
Vite package reported an error [rollup plugin dynamic import variables] unexpected token. It turned out that it was because of console log
自动编码器的相关内容
Compile php7 to specify the Remi extension directory and the configuration directory of ini
oracle ebs form表单常用对象及其关系
显示隐藏密码明文+密码框验证信息
6.ES5新增的数组的方法?
How to install Google play store in Xiaomi 10
js中的转义字符?
Achieve div selection effect
Determine whether binary search tree
Implementing DDD based on ABP -- domain service, application service and dto practice
Unmapped characters encoding GBK
Musk claims to be on the brain. Is it science or deception?
Thesis reading: ctrl: a conditional transformer language model for controllable generation
openfegin/restemplate微服务调用最佳实践总结
Judge whether to balance binary tree