from django.shortcuts importrender, redirect#Create your views here.
from django.contrib importauthfrom .models import *
importdatetimedeflogin(request):if request.method == "POST":
user= request.POST.get('user')
pwd= request.POST.get('pwd')
user= auth.authenticate(username=user, password=pwd)ifuser:
auth.login(request, user)#request.user
return redirect("/index/")return render(request, "login.html")defindex(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 inroom_list:
htmls+= "
{}({})".format(room.caption, room.num)for time_choice intime_choices:flag=Falsefor book inbook_list:if book.room.pk == room.pk and book.time_id ==time_choice[0]:
flag=Truebreak #意味着这个单元格已经被预定;
ifflag:if request.user.pk ==book.user.pk:
htmls+= "
{}".format(room.pk, time_choice[0],book.user)else:
htmls+= "
{}".format(room.pk,time_choice[0],
book.user)else:
htmls+= "
".format(room.pk, time_choice[0],)
htmls+= "
"print(htmls)return render(request, "index.html", locals())