pythonのdjango初体验

时间:2021-10-19 10:29:23

简单的一个列表展示,实现了增、删、插

1.通过新建项目来创建一个Django项目

2.通过pycharm中的Terminal来创建app   ,命令如下:

python manage.py startapp devicesys

最后项目结构如下:

项目结构:

pythonのdjango初体验

3.修改相关配置:

  在于项目同名的文件夹下打开setting.py 做两个事情(1)注释MIDDLEWARE中的   # 'django.middleware.csrf.CsrfViewMiddleware',  (2) 在文件结尾处增加STATICFILES_DIRS = [( os.path.join(BASE_DIR,"static"))]

  打开urls.py将devicesys导入进来

pythonのdjango初体验

4.在devicesys文件夹下的models.py中编写如下代码

 from django.db import models
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,DATE,ForeignKey,String
from sqlalchemy.orm import sessionmaker,relationship
# Create your models here. # 创建数据库连接
engine = create_engine("mysql+pymysql://root:root@localhost/testuser?charset=utf8",echo=True) # 生成orm基类
Base = declarative_base() # 创建表映射
class Devices(Base):
__tablename__ = "devices" # 表名称
id = Column(Integer,primary_key=True)
name = Column(String(64)) # 设备名称
ip = Column(String(32)) # 设备地址
username = Column(String(32)) # 设备账号
password = Column(String(32)) # 设备密码
brand = Column(String(32)) # 设备品牌
addtime = Column(DATE) # 设备添加时间 # 构建表结构
Base.metadata.create_all(engine) # 创建数据库会话类
session_class = sessionmaker(bind=engine)
# 创建数据库会话实例
session = session_class() def getlist():
return session.query(Devices).filter_by().all() def add(name,ip,username,password,brand):
temp = Devices(name=name,ip=ip,username=username,password=password,brand=brand)
session.add_all([temp])
session.commit()
def delete(del_id):
del_info = session.query(Devices).filter(Devices.id==del_id).first()
if del_info is not None:
session.delete(del_info)
session.commit()

在views.py中编写如下代码:

 from django.shortcuts import render

 # Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
from devicesys import models
import time
def loadlist(request):
list = models.getlist()
print(list)
return render(request,"index.html",{"list":list}) def add(request):
if request.method == "POST":
# 获取提交信息
name = request.POST.get("name")
ip = request.POST.get("ip")
username = request.POST.get("username")
password = request.POST.get("password")
brand = request.POST.get("brand")
# 获取当前时间并进行格式化
# addtime = time.strftime("%Y-%m-%d", time.time())
models.add(name,ip,username,password,brand)
list = models.getlist()
return render(request, "index.html", {"list": list}) def delete(request):
if request.method == "POST":
# 获取参数
del_id = request.POST.get("del_id")
if del_id:
models.delete(del_id)
list = models.getlist()
return render(request, "index.html", {"list": list})
else:
return HttpResponse("<h1>404</h1>")

5.在项目下创建static文件夹并且将layer相关文件导入进去,并且导入jquery.js

pythonのdjango初体验

6.在templates文件夹下新建index.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>服务器管理</title>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<script src="/static/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/layui/lay/modules/element.js"></script>
<script src="/static/layui/lay/modules/layer.js"></script>
<script>
//注意:导航 依赖 element 模块,否则无法进行功能性操作
layui.use('element', function(){
var element = layui.element;
});
</script>
</head>
<body>
<ul class="layui-nav" lay-filter="">
<li class="layui-nav-item"><a href="">最新活动</a></li>
<li class="layui-nav-item layui-this"><a href="">产品</a></li>
<li class="layui-nav-item"><a href="">大数据</a></li>
<li class="layui-nav-item">
<a href="javascript:;">解决方案</a>
<dl class="layui-nav-child"> <!-- 二级菜单 -->
<dd><a href="">移动模块</a></dd>
<dd><a href="">后台模版</a></dd>
<dd><a href="">电商平台</a></dd>
</dl>
</li>
<li class="layui-nav-item"><a href="">社区</a></li>
</ul>
<div class="layui-form-item" style="float:left;padding-top: 10px;">
<div class="layui-input-block" style="float:left;margin-left: 10px;">
<input type="text" name="title" required lay-verify="required" placeholder="关键字搜索" autocomplete="off" class="layui-input">
</div>
<button class="layui-btn layui-btn-normal" style="padding-left:5px; ">
<i class="layui-icon"></i>
</button>
</div>
<div class="layui-btn-group" style="float:right;padding-top: 10px;padding-bottom: 10px;padding-right: 10px;">
<button onclick="add_win()" class="layui-btn layui-btn-normal">
<i class="layui-icon"></i>
</button>
</div>
<div>
<table class="layui-table">
<colgroup>
<col width="150">
<col width="200">
<col>
</colgroup>
<thead>
<tr>
<td>序号</td>
<td>名称</td>
<td>IP地址</td>
<td>账号</td>
<td>品牌</td>
<td>操作</td>
</tr>
</thead>
<tbody>
{% for row in list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.name }}</td>
<td>{{ row.ip }}</td>
<td>{{ row.name }}</td>
<td>{{ row.brand }}</td>
<td>
<a class="del" href="#" row-id="{{ row.id }}">删除 |</a>
<a href="#" row-id="{{ row.id }}">详情</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div style="display:none;margin-top: 10px;" id="add_win">
<form class="layui-form" action="/add/" method="post">
<div class="layui-form-item">
<label class="layui-form-label">设备名称</label>
<div class="layui-input-block">
<input type="text" name="name" style="width: 260px;" required lay-verify="required" placeholder="请输入设备名称" autocomplete="off" class="layui-input">
</div>
<label class="layui-form-label">IP地址</label>
<div class="layui-input-block">
<input type="text" name="ip" style="width: 260px;" required lay-verify="required" placeholder="请输入IP地址" autocomplete="off" class="layui-input">
</div>
<label class="layui-form-label">账号</label>
<div class="layui-input-block">
<input type="text" name="username" style="width: 260px;" required lay-verify="required" placeholder="请输入设备账号" autocomplete="off" class="layui-input">
</div>
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="text" name="password" style="width: 260px;" required lay-verify="required" placeholder="请输入设备密码" autocomplete="off" class="layui-input">
</div>
<label class="layui-form-label">品牌</label>
<div class="layui-input-block">
<input type="text" name="brand" style="width: 260px;" required lay-verify="required" placeholder="请输入设备品牌" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<div style="display:none;margin-top: 10px;" id="del_win">
<form class="layui-form" action="/del/" method="post">
<div class="layui-form-item">
<span style="margin-left:30px;">您确定要删除该条数据?</span>
<input type="hidden" id="del_id" name="del_id"/>
</div>
<div class="layui-form-item">
<div class="layui-input-block" style="margin-left: 45px;margin-top: 20px;">
<button class="layui-btn" lay-submit lay-filter="formDemo">确定</button>
<button type="button" id="cancel" class="layui-btn layui-btn-primary">取消</button>
</div>
</div>
</form>
</div>
<script>
$(function(){
$(".del").click(function(){
var row_id = $(this).attr('row-id');
$("#del_id").val(row_id);
layer.open({
type:1,
area: ['220px', '150px'],
shadeClose: true,
title:"删除",
end :function(index, layero){
$("#del_id").val("")
$("#del_win").css('display','none')
},
content:$("#del_win").css('display','block')
})
});
$("#cancel").click(function () {
layer.close(layer.index);
});
})
function add_win(){
layer.open({
type:1,
area: ['500px', '400px'],
shadeClose: true,
title:"新增",
end :function(index, layero){
$("#add_win").css('display','none')
},
content:$("#add_win").css('display','block')
})
}
</script>
</body>
</html>

运行结果如下:

pythonのdjango初体验

注:目前仅实现了新增和删除,搜索和详情未实现。