1. 程式人生 > >會議室預定demo

會議室預定demo

from 知識 bug 127.0.0.1 con 年月日 views post ext

關於會議室的增刪改查

查:

HTML:

login繼承django自帶的admin用戶認證系統

技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<form action="" method="post">
    {% csrf_token %}
    <p>姓名 <input type="text"
name="user"></p> <p>密碼 <input type="password" name="pwd"></p> <input type="submit"> </form> </body> </html>
login 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible
" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css"> <script src="/static/jquery-3.2.1.min.js"></script> <title>會議室</title> <style > .active { background
-color: deepskyblue !important; color: black; text-align: center; font-size: 16px; } .td_active { background-color: greenyellow ; } .active_other { background-color: orange !important; color: white; text-align: center; font-size: 16px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-11"> <h3>會議室預定</h3> <div> <table class="table table-bordered table-striped"> <thead> <tr> <th>會議室</th> {# 時間 時間元組 #} {% for item in time_choices %} <th>{{ item.1 }}</th> {% endfor %} </tr> </thead> <tbody> {{ html|safe }} </tbody> </table> <button class="btn btn-primary pull-right keep">保存</button> </div> </div> </div> </div> <script> </script> </body> </html>
index.

PY:

技術分享圖片
from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.
class MeetingRoom(models.Model):
    ‘‘‘會議室 ‘‘‘
    name = models.CharField(max_length=32,verbose_name="會議室名稱")
    num = models.IntegerField()     # 最大開會人數


    def __str__(self):
        return self.name


class UserInfo(AbstractUser):
    tel=models.CharField(max_length=32)


    def __str__(self):
        return self.username

class Book(models.Model):
    ‘‘‘預定記錄表‘‘‘
    date = models.DateField(verbose_name="預定日期")
    user = models.ForeignKey(to="UserInfo",verbose_name="預訂用戶")         # 關聯用戶
    room = models.ForeignKey(to="MeetingRoom",verbose_name="預定房間")      # 關聯房間
    time1 = (
        (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"),
    )
    timeline = models.IntegerField(choices=time1,verbose_name="預定時間")    # 存的是數字

    class Meta:
        # 聯合唯一,為什麽沒有user,因為只有有下面3個字段,即表示有預定了
        unique_together = (
            (room,date,timeline),
        )


    def __str__(self):
        return str(self.user) + "預定了" + str(self.room)
model 技術分享圖片
from django.shortcuts import render,redirect

# Create your views here.
from .models import *

def index(request):
    #   取到所有的預定信息
    book_list = Book.objects.all()
    #   取所有的房間信息
    room_list = MeetingRoom.objects.all()
    #   房間的時間段
    time_choices = Book.time1


    # 渲染空的td, 有幾個td,取決於有幾個時間段
    html=""
    for room in room_list:
        s = "<tr><td>{0}({1})</td>".format(room.name,room.num)
        for item in time_choices:  #  循環所有的時間段單元格 ((1,"8:00"))()
            flag=False      # 標誌有否預定信息
            for book in book_list:      # 循環每個預定信息
                print(MeetingRoom.pk)
                if book.room.pk == room.pk and book.timeline == item[0]:

                    flag=True # 通過
                    break
            if flag:
                # 最後循環出來的book是匹配的信息
                if request.user.pk != book.user.pk:  # 不同用戶顯示不同的樣式
                    s += <td class="active_other item" room_id="{0}" time_id="{1}">{2}</td>.format(room.pk,item[0],book.user.username)
                else:
                    s += <td class="active item" room_id="{0}" time_id="{1}">{2}</td>.format(room.pk,item[0],book.user.username)
            else:
                s += <td class="item" room_id="{0}" time_id="{1}"></td>.format(room.pk,item[0])
        s += "</tr>"
        html += s
    return render(request,"index.html",locals())

from django.contrib import auth

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)   # 註冊session
            return redirect("/index/")

    return render(request, "login.html")
views

關於DATE--轉化-->年月日

也可以通過CHOSEN_DATE=new Date().getFullYear()等等,各取出年月日,拼成年月日

技術分享圖片

知識點:js

自定義標簽方法

技術分享圖片

技術分享圖片

-------------

技術分享圖片

datetime.date---->年月日

datetime.time----->時分秒

datetime.datetime---->年月日時分秒

BUG1

使用時間插件跳轉某日期,由於ajax,刷新頁面導致date重新賦值 一直是當前日期。

思路:直接取url的值http://127.0.0.1:8000/index/?book_date=2018-03-29

會議室預定demo