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的过程中少走弯路.整个课程以实例教学为核心,通过对大量丰富的经典实例的讲解.让 ...
 
随机推荐
- python logging 模块记录日志
			
#日志记录到多文件示例 import logging def error_log(message): file_1_1 = logging.FileHandler('error.log', 'a+', ...
 - 六、C++离散傅里叶逆变换
			
C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...
 - 有些其他程序设置为从 Outlook 下载并删除邮件。为防止发生此意外情况,我们将这些邮件放入一个特殊的 POP 文件夹中
			
最近使用FOXMAIL接收MSN邮件时,发现有一些邮件收取不到,进到WEB页面,页面下方提示“你的邮件位于 POP 文件夹中!有些其他程序设置为从 Outlook 下载并删除邮件.为防止发生此意外情况 ...
 - Http协议--请求报文和响应报文
			
http协议是位于应用层的协议,我们在日常浏览网页比如在导航网站请求百度首页的时候,会先通过http协议把请求做一个类似于编码的工作,发送给百度的服务器,然后在百度服务器响应请求时把相应 ...
 - 一、Web 如何工作的
			
平常我们在浏览器中输入一个网址,随即看到一个页面,这个过程是怎样实现的呢?下面用一幅图来说明: 整个流程如下: 1.域名解析 浏览器会解析域名对应的IP地址 PS:DNS服务器的知识 2.建立TCP ...
 - IPC Gateway  设计
			
1. IPC Gateway对外提供的功能: IPC的register/request/reply/notification服务. 2. IPC Gatew的实现原理: 各个具体的服务注册自己的回调函 ...
 - 将数据库数据添加到ListView控件中
			
实现效果: 知识运用: ListView控件中的Items集合的Clear方法 //从listView控件的数据项集合中移除所有数据项 补充:可以使用Remove或RemoveAt方法从集合中移除单个 ...
 - 漫谈 Clustering (5): Hierarchical Clustering
			
系列不小心又拖了好久,其实正儿八经的 blog 也好久没有写了,因为比较忙嘛,不过觉得 Hierarchical Clustering 这个话题我能说的东西应该不多,所以还是先写了吧(我准备这次一个公 ...
 - python操作文件目录
			
# 查看当前目录的绝对路径: >>> os.path.abspath('.') /Users/NaCl/Documents/GitHub #同样的道理,要拆分路径时,也不要直接去拆字 ...
 - 问题008:java 中代码块的风格有几种?单行注释可否嵌套?多行注释可否嵌套?
			
有两种:一种是次行风格,英文称为next-line 一种是是行尾风格,英文称为 end-of-line 举例 行尾风格 public class HelloWorld{ public static v ...
 
			
		