from django.shortcuts import render,redirect
from django.views import View
from jishi.models import User,Class
from django.http import HttpResponse
# Create your views here.
class register(View):
def get(self,request):
return render(request,‘register.html‘,locals())
def post(self,request):
mes = ""
name = request.POST.get(‘name‘)
pwd = request.POST.get(‘pwd‘)
permission = request.POST.get(‘permission‘)
if all([name,pwd,permission]):
User.objects.create(
name= name,
pwd= pwd,
permission= permission
)
mes = ‘添加成功‘
else:
mes = ‘数据不完整‘
return render(request, ‘register.html‘, locals())
class login(View):
def get(self,request):
return render(request,‘login.html‘,locals())
def post(self,request):
mes = ""
name = request.POST.get(‘name‘)
pwd = request.POST.get(‘pwd‘)
if all([name,pwd]):
user = User.objects.filter( name = name).first()
if user:
if user.pwd == pwd:
request.session[‘name‘]= user.name
request.session[‘id‘] = user.id
return redirect(‘/add_class/‘)
else:
mes = ‘密码不正确‘
else:
mes = ‘用户名不存在‘
else:
mes = ‘信息不全‘
return render(request, ‘login.html‘, locals())
from lianxi.settings import UPLOAD_ROOT
import os
def uploadfile(img):
f = open(os.path.join(UPLOAD_ROOT,‘‘,img.name),‘wb‘)
#写文件 遍历图片文件流
for chunk in img.chunks():
f.write(chunk)
class add_class(View):
def get(self, request):
id = request.session.get(‘id‘)
user = User.objects.filter(id =id).first()
return render(request,‘add_class.html‘,locals())
def post(self, request):
id =request.session.get(‘id‘)
name = request.POST.get(‘name‘)
img = request.FILES.get(‘image‘)
num = request.POST.get(‘num‘)
uploadfile(img)
c =Class.objects.create(
u_id_id= id,
name = name,
image= ‘/upload/‘ + img.name,
num=num
)
return redirect(‘/add_class/‘)
from django.core.paginator import Paginator
from utils.comm import PRECATECOUNT
class index(View):
def get(self,request):
id = request.session.get(‘id‘)
c = Class.objects.order_by(‘-num‘).all()
# courls = Courls.objects.all()
# 每一页可以展示多少条数据
page_count = PRECATECOUNT
# 默认为第一页
current_page = int(request.GET.get(‘p‘, 1))
g = Paginator(c, page_count)
# 每页显示的信息
clist = g.get_page(current_page)
# 一共显示多少页
total_page = g.num_pages
return render(request, "index.html", {"c": c, "clist": clist, "total_page": total_page,
"current_page": current_page, "id": id})
class delete(View):
def get(self,request):
# user_id = request.session.get("id")
# print(user_id)
id = request.GET.get("id")
print(id)
c = Class.objects.filter(id = id).delete()
return redirect(‘/‘)