def handle_classes(request): # 请求数据 if request.method == 'GET': # for i in range(100): # models.Classes.objects.create(caption="三年二班" + str(i)) current_user = request.session.get('username') # 添加一部分测试数据 # models.Classes.objects.create(caption="三年七班") # models.Classes.objects.create(caption="三年二班") # models.Classes.objects.create(caption="三年四班") # 获取所有的班级列表 # cls_list = models.Classes.objects.all() # 当前页 current_page = request.GET.get('p', 1) current_page = int(current_page) # 所有数据的个数 total_count = models.Classes.objects.all().count() # 引入分页函数 from utils.page import PagerHelper # 总数,页码,当前分页url,页数 obj = PagerHelper(total_count, current_page, '/classes.html', 10) pager = obj.pager_str() cls_list = models.Classes.objects.all()[obj.db_start:obj.db_end] # from django.utils.safestring import mark_safe -> 渲染django return render(request, 'classes.html', { 'username': current_user, 'cls_list': cls_list, 'str_pager': pager }) # 添加数据 elif request.method == 'POST': # Form表单的提交方式 ''' caption = request.POST.get('caption', None) # 默认为None models.Classes.objects.create(caption=caption) return redirect('/classes.html') ''' # ajax的请求处理 response_dict = {'status': True, 'error': None, 'data': None} caption = request.POST.get('caption', None) if caption: # 新增数据 obj = models.Classes.objects.create(caption=caption) response_dict['data'] = {'id': obj.id, 'caption': obj.caption} else: response_dict['status'] = False response_dict['error'] = '标题不能为空' # json格式 -> ajax返回数据前端作处理 return HttpResponse(json.dumps(response_dict))
def handle_classes(request): if request.method == "GET": # for i in range(1000): # models.Classes.objects.create(caption='全栈一班'+str(i)) # 获取所有的班级列表 # models.Classes.objects.create(caption='全栈一班') # models.Classes.objects.create(caption='全栈二班') # models.Classes.objects.create(caption='全栈三班') # 当前页 current_page = request.GET.get('p', 1) current_page = int(current_page) # 所有数据的个数 total_count = models.Classes.objects.all().count() from utils.page import PagerHelper obj = PagerHelper(total_count, current_page, '/classes.html', 5) pager = obj.pager_str() cls_list = models.Classes.objects.all()[obj.db_start:obj.db_end] current_user = request.session.get('username') return render(request, 'classes.html', { 'username': current_user, 'cls_list': cls_list, 'str_pager': pager }) elif request.method == "POST": # Form表单提交的处理方式 """ caption = request.POST.get('caption',None) if caption: models.Classes.objects.create(caption=caption) return redirect('/classes.html') """ # Ajax import json response_dict = {'status': True, 'error': None, 'data': None} caption = request.POST.get('caption', None) print(caption) if caption: obj = models.Classes.objects.create(caption=caption) response_dict['data'] = {'id': obj.id, 'caption': obj.caption} else: response_dict['status'] = False response_dict['error'] = "标题不能为空" return HttpResponse(json.dumps(response_dict))
def handle_student(request): if request.method == "GET": # for i in range(1,100): # Student.objects.create(name='root' + str(i), # email='*****@*****.**' + str(i), # cls_id=i) username = request.session.get("username") # 封装类的方式实现分页 from utils.page import PagerHelper current_page = request.GET.get('p', 1) current_page = int(current_page) total_count = Student.objects.all().count() obj = PagerHelper(total_count, current_page, '/student', 10) result = Student.objects.all()[obj.db_start:obj.db_end] pager = obj.pager_str() return render(request, 'student.html', { 'username': username, 'result': result, 'str_pager': pager })
def handle_classes(request): if request.method == "GET": """ #返回所有色数据 current_user = request.session.get('username') #从数据库中获取信息 cls_list = Classes.objects.all() return render(request, 'classes.html', {'username': current_user,'cls_list': cls_list}) """ """ '''初步实现分页功能 10:每页显示10个页码 current_page:当前页页码 start:起始页码 end: 结束页码 cls_list:从数据库中获取的部分数据 total_count:数据库中总数 v:总页数 pager_list:存放处理后带html代码(一些a标签)列表 ''' #第一次请求p=1 current_page = request.GET.get('p', 1) current_page = int(current_page) # current_page start end # 1, 0, 10 # 2, 10, 20 # 3, 20, 30 start = (current_page - 1) * 10 end = current_page * 10 cls_list = Classes.objects.all()[start:end] total_count = Classes.objects.all().count() pager_list = [] #计算一共多少页 v表示总页数 v, a = divmod(total_count, 10) if a != 0: v += 1 pager_list.append('<a href="/classes?p=%s">上一页</a>' % (current_page - 1,)) for i in range(1,v+1): if i == current_page: #为当前页加acative样式 pager_list.append('<a class="pageactive" href="/classes?p=%s">%s</a>' % (i, i,)) else: pager_list.append('<a href="/classes?p=%s">%s</a>' % (i, i,)) pager_list.append('<a href="/classes?p=%s">下一页</a>' % (current_page + 1,)) pager = "".join(pager_list) # mark_safe(pager) #print(pager) """ #封装类的方式实现分页 from utils.page import PagerHelper current_page = request.GET.get('p', 1) current_page = int(current_page) total_count = Classes.objects.all().count() obj = PagerHelper(total_count, current_page, '/classes', 10) cls_list = Classes.objects.all()[obj.db_start:obj.db_end] pager = obj.pager_str() current_user = request.session.get('username') return render(request, 'classes.html', { 'username': current_user, 'cls_list': cls_list, 'str_pager': pager }) # 处理前端通过ajax方式提交过来数据 elif request.method == "POST": #设置字典存放信息,传送到前端,前端根据返回信息不同做相应处理 response_dict = {'status': True, 'error': None, 'data': None} caption_name = request.POST.get('caption', None) if caption_name: obj = Classes.objects.create( caption=caption_name ) #obj 是一个类Classes object (5), obj.id是添加后自增id ,obj.caption是班级名称 #print(obj.id,obj.caption) response_dict['data'] = {"id": obj.id, "caption": obj.caption} else: response_dict['status'] = False response_dict['error'] = "标题不能为空" return HttpResponse(json.dumps(response_dict))
def index(request): if request.method == "GET": from django.core.handlers.wsgi import WSGIRequest print(request.environ["HTTP_USER_AGENT"]) # 分页 current_page = request.GET.get('p', 1) current_page = int(current_page) # 取出数据库 数据总数 total_count = models.Article.objects.all().count() from utils.page import PagerHelper obj = PagerHelper(total_count, current_page, '/index.html') pager = obj.pager_str() status_auth = { 'username': request.session.get("username"), 'is_login': request.session.get('is_login') } user_id = models.Userinfo.objects.filter( username=status_auth["username"]).values("id").first() request.session["user_id"] = user_id articles = models.Article.objects.all().order_by( "-id")[obj.db_start:obj.db_end].values( "id", "title", "description", "content", "img", "utime", "imgdescription", "t_id", "t__caption", "web_url", "who__username", "favor_count") # [::-1] tags = models.Tags.objects.all() current_time = time.time() # favor_count = models.Userinfo.objects. return render( request, 'index.html', { "userinfo": status_auth, "articles": articles, 'tags': tags, "current_time": int(current_time), 'str_pager': pager, "user_id": user_id }) elif request.method == "POST": result = { 'status': True, 'error': None, 'data': None, 'authocede': None, 'username': None, 'passowrd': None, 'messagess': None, } obj = Login(request.POST) if obj.is_valid(): # clean 是拿到成功的数据为dict类型 value_dict = obj.clean() username = value_dict['username'] password = value_dict['password'] # 判断用户名和密码是否存在~ if models.Userinfo.objects.filter(username=username, password=password).count(): # 生成随机字符串验证码 random_int = random.randint(1000, 9999) # 验证码并且返回给用户 result['authocede'] = str(random_int) request.session['username'] = username request.session['is_login'] = True models.Userinfo.objects.filter(username=username).update( authcode=str(random_int)) result['username'] = username result['password'] = password else: result['status'] = False result['username'] = username result['passowrd'] = password result['messagess'] = "用户名和密码不一致~" return HttpResponse(json.dumps(result)) else: error_obj = obj.errors.as_json() result['status'] = False result['error'] = error_obj return HttpResponse(json.dumps(result))
def handle_student(request): #先拿到当前登录人的名字 current_user = request.session.get('username') #拿到当前登录人的身份 identity = request.session.get('identity') #根据不同身份返回不同界面: if identity == "root": #管理员返回全部学生列表 if request.method == "GET": # 获取当前页,如果没传默认是1 current_page = request.GET.get('p', 1) current_page = int(current_page) # 所有数据的个数 total_count = models.classes.objects.all().count() print("数据个数=" + str(total_count)) from utils.page import PagerHelper # 创建中间人obj,中间人调用pager_str()方法,经过一系列处理后把页码标签返回给你,赋值给变量pager obj = PagerHelper(total_count, current_page, '/student.html', 10) pager = obj.pager_str() student_list = models.student.objects.all()[obj.db_start:obj. db_end] current_user = request.session.get('username') return render( request, 'student.html', { 'student_list': student_list, 'username': current_user, 'str_pager': pager }, ) if identity == "teacher": #老师返回自己管理班级的信息 pass if identity == "student": #学生返回自己的信息 User_unique_ID = request.session.get('User_unique_ID') #拿到学生的用户唯一ID,如果在学生表里能找到这个ID,就说明已经完善过学生信息了,直接显示就OK, #如果找不到,就显示现有信息,然后完善,补充到学生表去。 c = models.student.objects.filter(student_id=User_unique_ID).count() if c: if request.method == "GET": #显示自己的学生信息 # 获取当前页,如果没传默认是1 current_page = request.GET.get('p', 1) current_page = int(current_page) # 数据的个数 total_count = c from utils.page import PagerHelper # 创建中间人obj,中间人调用pager_str()方法,经过一系列处理后把页码标签返回给你,赋值给变量pager obj = PagerHelper(total_count, current_page, '/classes.html', 10) pager = obj.pager_str() student_list = models.student.objects.filter( student_id=User_unique_ID) current_user = request.session.get('username') return render( request, 'student.html', { 'student_list': student_list, 'username': current_user, 'str_pager': pager }, ) else: message = "没有找到您的信息,请点此完善" return render(request, 'Error_pages.html', { 'User_unique_ID': User_unique_ID, 'msg': message }) else: return redirect('/index.html')
def handle_class(request): # 如果是GET请求访问说明是访问页面,正常渲染数据库里面的内容。 if request.method == "GET": # 获取当前页,如果没传默认是1 current_page = request.GET.get('p', 1) current_page = int(current_page) # 所有数据的个数 total_count = models.classes.objects.all().count() print("数据个数=" + str(total_count)) from utils.page import PagerHelper #创建中间人obj,中间人调用pager_str()方法,经过一系列处理后把页码标签返回给你,赋值给变量pager obj = PagerHelper(total_count, current_page, '/classes.html', 10) pager = obj.pager_str() clss_list = models.classes.objects.all()[obj.db_start:obj.db_end] current_user = request.session.get('username') return render( request, 'classes.html', { 'clsss_list': clss_list, 'username': current_user, 'str_pager': pager }, ) #如果是POST请求说明是提交数据,获取到前端提取的caption值, #如果不为空的话添加到数据库里,把数据更新到响应列表,再把这个响应列表返回给前端, # 再追加在表单末端,个人觉得这种方式很鸡肋,不喜欢。 elif request.method == "POST": # Ajax import json #status,状态。 error,错误。data,数据。demand,需求。 response_dict = { 'status': True, 'error': None, 'data': None, 'demand': None } caption = request.POST.get('caption', None) demand = request.POST.get('demand', None) print("需求" + demand) # 先判断需求是啥子: if demand == "add": if caption: obj = models.classes.objects.create(caption=caption) response_dict['data'] = {'id': obj.id, 'caption': obj.caption} else: response_dict['status'] = False response_dict['error'] = "标题不能为空" return HttpResponse(json.dumps(response_dict)) if demand == "edit": nid = request.POST.get('id') caption = request.POST.get('caption') print(nid, caption) if caption: models.classes.objects.filter(id=nid).update(caption=caption) response_dict['error'] = True else: response_dict['status'] = False response_dict['error'] = "标题不能为空" return HttpResponse(json.dumps(response_dict)) if demand == "Delete": nid = request.POST.get('id') print(nid, demand) if nid: models.classes.objects.filter(id=nid).delete() response_dict['error'] = True else: response_dict['status'] = False response_dict['error'] = "删除失败,请重新操作。" return HttpResponse(json.dumps(response_dict)) else: return redirect('/classes.html')
def handle_teacher(request): # 先拿到当前登录人的名字 current_user = request.session.get('username') # 拿到当前登录人的身份 identity = request.session.get('identity') # 拿到登录人的ID User_unique_ID = request.session.get('User_unique_ID') if request.method == "GET": #显示老师和他教的所有的班级。 # 获取当前页,如果没传默认是1 current_page = request.GET.get('p', 1) current_page = int(current_page) # 所有数据的个数 total_count = models.teacher.objects.all().count() print("数据个数=" + str(total_count)) from utils.page import PagerHelper # 创建中间人obj,中间人调用pager_str()方法,经过一系列处理后把页码标签返回给你,赋值给变量pager obj = PagerHelper(total_count, current_page, '/teacher.html', 10) pager = obj.pager_str() #一次性把老师列表全部都拿到 teacher_list = models.teacher.objects.filter( id__in=models.teacher.objects.all()).values( 'id', 'name', 'cls__id', 'cls__caption') result = {} for t in teacher_list: if t['id'] in result: if t['cls__id']: result[t['id']]['cls_list'].append({ 'id': t['cls__id'], 'caption': t['cls__caption'] }) else: if t['cls__id']: temp = [ { 'id': t['cls__id'], 'caption': t['cls__caption'] }, ] else: temp = [] result[t['id']] = { 'nid': t['id'], 'name': t['name'], 'cls_list': temp } return render(request, 'teacher.html', { 'username': current_user, 'pager': pager, 'teacher_list': result }) else: return render(request, 'teacher.html', {'username': current_user})
def handle_classes(request): if request.method == "GET": current_user = request.session.get('username') #cls_list = models.Classes.objects.all() #return (request,'classes.html',{'username':current_user,'cls_list':cls_list}) current_page = request.GET.get('p', 1) current_page = int(current_page) print(current_page) # 所有数据的个数 total_count = models.Classes.objects.all().count() from utils.page import PagerHelper obj = PagerHelper(total_count, current_page, '/classes.html', 5) pager = obj.pager_str() cls_list = models.Classes.objects.all()[obj.db_start:obj.db_end] ''' # 1, 0,10 # 2, 10,20 # 3, 20,30 start = (current_page - 1) * 10 end = current_page * 10 cls_list = models.Classes.objects.all()[start:end] total_count = models.Classes.objects.all().count() # 99 # 每页显示10条数据 # total_count v,a = divmod(total_count,10) if a != 0: v += 1 pager_list = [] pager_list.append('<a href="/classes.html?p=%s">上一页</a>' % (current_page-1, )) for i in range(1,v+1): if i == current_page: pager_list.append('<a class="active" href="/classes.html?p=%s">%s</a>'% (i, i,)) else: pager_list.append('<a href="/classes.html?p=%s">%s</a>' %(i,i,)) pager_list.append('<a href="/classes.html?p=%s">下一页</a>' % (current_page + 1,)) pager = "".join(pager_list) ''' return render(request, 'classes.html', { 'username': current_user, 'cls_list': cls_list, 'str_pager': pager }) elif request.method == "POST": import json response_dict = {'status': True, 'error': None, 'data': None} caption = request.POST.get('caption', None) if caption: obj = models.Classes.objects.create(caption=caption) response_dict['data'] = { 'id': obj.id, 'caption': obj.caption, } else: response_dict['status'] = False response_dict['error'] = '标题不能为空' return HttpResponse(json.dumps(response_dict))