Python-S9—Day86-ORM项目实战之会议室预定相关
01 会议室预定1
02 会议室预定2
03 会议室预定3
04 会议室预定4
05 会议室预定5
06 会议室预定6
01 会议室预定1
1.1 项目的架构目录;

1.2 使用Pycharm创建Django项目:MRBS并建立引用app01(Django版本指向全局解释器,为Django =“1.11.1”)
1.3 settings.py中配置STATICFILES_DIRS以及AUTH_USER_DIRS
AUTH_USER_MODEL = "app01.UserInfo"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
1.4 models.py中配置ORM并在admin.py中进行注册;
from django.db import models # Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser):
tel = models.CharField(max_length=32) class Room(models.Model):
"""
会议室表;
"""
caption = models.CharField(max_length=32)
num = models.IntegerField() # 容纳的人数; def __str__(self):
return self.caption class Book(models.Model):
"""
会议室预定信息;
"""
user = models.ForeignKey("UserInfo", on_delete=models.CASCADE) # 级联删除;
room = models.ForeignKey("Room", on_delete=models.CASCADE)
date = models.DateField()
time_choices = (
(1, "8:00"),
(2, "9:00"),
(3, "10:00"),
(4, "11:00"),
(5, "12:00"),
(6, "13:00"),
(7, "14:00"),
(8, "15:00"),
(9, "16:00"),
(10, "17:00"),
(11, "18:00"),
(12, "19:00"),
(13, "20:00"),
)
time_id = models.IntegerField(choices=time_choices) # class Meta:
unique_together = (
('room', 'date', 'time_id')
) # 联合唯一! def __str__(self):
return str(self.user) + "预定了" + str(self.room)
1.5 配置urls.py;
"""MRBS URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', views.login),
url(r'^index/', views.index),
]
1.6 编写views.py视图函数;
from django.shortcuts import render, redirect # Create your views here.
from django.contrib import auth
from .models import *
import datetime def login(request):
if request.method == "POST":
user = request.POST.get('user')
pwd = request.POST.get('pwd')
user = auth.authenticate(username=user, password=pwd)
if user:
auth.login(request, user) # request.user
return redirect("/index/")
return render(request, "login.html") def index(request):
date = datetime.datetime.now().date()
book_date = request.GET.get("book_date", date)
time_choices = Book.time_choices
room_list = Room.objects.all()
book_list = Book.objects.filter(date=book_date) htmls = ""
for room in room_list:
htmls += "<tr><td>{}({})</td>".format(room.caption, room.num)
for time_choice in time_choices:
flag = False
for book in book_list:
if book.room.pk == room.pk and book.time_id == time_choice[0]:
flag = True
break # 意味着这个单元格已经被预定;
if flag:
if request.user.pk == book.user.pk:
htmls += "<td class = 'active' room_id={} time_id={} >{}</td>".format(room.pk, time_choice[0],
book.user)
else:
htmls += "<td class = 'another_active' room_id={} time_id={} >{}</td>".format(room.pk,
time_choice[0],
book.user)
else:
htmls += "<td room_id={} time_id={} ></td>".format(room.pk, time_choice[0],
)
htmls += "</tr>" print(htmls)
return render(request, "index.html", locals())
1.7 编写templates;
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
<script src="/static/js/jquery-1.12.4.min.js"></script>
<script src="/static/datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script src="/static/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>
<style>
.active {
background-color: green !important;
color: white;
} .another_active {
background-color: #2b669a;
color: white;
} .td_active {
background-color: lightblue;
color: white;
}
</style>
</head>
<body>
<h3>会议室预定</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>会议室/时间</th>
{% for time_choice in time_choices %}
<th>{{ time_choice.1 }}</th>
{% endfor %}
</tr> </thead>
<tbody>
{{ htmls|safe }}
</tbody>
</table>
</body>
</html>
1.8 创建static目录并添加jquery、Bootstrap以及datetimerpicker;

1.9 基本的页面展示如下:

02 会议室预定2
03 会议室预定3

04 会议室预定4
05 会议室预定5
06 会议室预定6
Python-S9—Day86-ORM项目实战之会议室预定相关的更多相关文章
- Python爬虫开发与项目实战
Python爬虫开发与项目实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1MFexF6S4No_FtC5U2GCKqQ 提取码:gtz1 复制这段内容后打开百度 ...
- Python爬虫开发与项目实战pdf电子书|网盘链接带提取码直接提取|
Python爬虫开发与项目实战从基本的爬虫原理开始讲解,通过介绍Pthyon编程语言与HTML基础知识引领读者入门,之后根据当前风起云涌的云计算.大数据热潮,重点讲述了云计算的相关内容及其在爬虫中的应 ...
- python工业互联网监控项目实战5—Collector到opcua服务
本小节演示项目是如何从连接器到获取Tank4C9服务上的设备对象的值,并通过Connector服务的url返回给UI端请求的.另外,实际项目中考虑websocket中间可能因为网络通信等原因出现中断情 ...
- python工业互联网监控项目实战4—python opcua
前面章节我们采用OPC作为设备到上位的信息交互的协议,本章我们介绍跨平台的OPC UA.OPC作为早期的工业通信规范,是基于COM/DCOM的技术实现的,用于设备和软件之间交换数据,最初,OPC标准仅 ...
- python金融反欺诈-项目实战
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- python工业互联网监控项目实战2—OPC
OPC(OLE for Process Control)定义:指为了给工业控制系统应用程序之间的通信建立一个接口标准,在工业控制设备与控制软件之间建立统一的数据存取规范.它给工业控制领域提供了一种标准 ...
- python数据分析美国大选项目实战(三)
项目介绍 项目地址:https://www.kaggle.com/fivethirtyeight/2016-election-polls 包含了2015年11月至2016年11月期间对于2016美国大 ...
- Python工业互联网监控项目实战3—websocket to UI
本小节继续演示如何在Django项目中采用早期websocket技术原型来实现把OPC服务端数据实时推送到UI端,让监控页面在另一种技术方式下,实时显示现场设备的工艺数据变化情况.本例我们仍然采用比较 ...
- Python轻松入门到项目实战-实用教程
本课程完全基于Python3讲解,针对广大的Python爱好者与同学录制.通过本课程的学习,可以让同学们在学习Python的过程中少走弯路.整个课程以实例教学为核心,通过对大量丰富的经典实例的讲解.让 ...
随机推荐
- dsniff
/usr/local/sbin/dsniff 这个东西好强大,获取到用户名和密码 bt服务区器上:dsniff -i eth0 -m(自动协议检测) 在另外一个电脑上打开网页,登陆ftp服务器,回头看 ...
- 关于第三方dll,ocx开发的思考
A问题: 最近有个工作,要集成一套老的指纹考勤机器到现在考勤系统(web系统)中,问题出现时老的机器只有ocx可用,没有可用的dll:原本以为简单的第三方调用就ok了,可是ocx不能被承载,在实现上费 ...
- Django Form 表单
Form 表单功能 生成HTML表单元素检查表单元素的合法性验证如果错误,重复显示表单数据类型转换 Form相关的对象 Widget 渲染成HTML元素的工具Field Form对象中的一个字段For ...
- vue登录权限
登录:当用户填写完账号和密码后向服务端验证是否正确,验证通过之后,服务端会返回一个token,拿到token之后(我会将这个token存贮到cookie中,保证刷新页面后能记住用户登录状态),前端会根 ...
- go语言,安装包fetch error 问题解决方案
最近需要安装grequests,出现了下面的error [fdf@zxmrlc ~]$ go get github.com/levigross/grequests package golang.org ...
- 2018.10.05 TOPOI提高组模拟赛 解题报告
得分: \(100+5+100=205\)(真的是出乎意料) \(T1\):抵制克苏恩(点此看题面) 原题: [BZOJ4832][Lydsy1704月赛] 抵制克苏恩 应该还是一个比较简单的\(DP ...
- 题解 CF734A 【Anton and Danik】
本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...
- Activiti学习记录(二)
1.初始化数据库 使用工作流引擎创建23张表 public class TestActiviti { /** * 使用代码创建工作流需要的23张表 */ @Test public void creat ...
- TCP/IP与OSI参考模型原理
网络是很重要同时也是很难理解的知识,这篇文章将会用自己容易理解的方式来记录有关网络的tcp与osi模型内容,不求专业深刻,但求通俗易懂也好. OSI参考模型 OSI定义了网络互连的七层框架(物理层.数 ...
- k8s的pv和pvc简述
pvc:资源需要指定:1.accessMode:访问模型:对象列表: ReadWriteOnce – the volume can be mounted as read-write by a s ...
