博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django 练习班级管理系统四 -- 编辑班级
阅读量:5236 次
发布时间:2019-06-14

本文共 6708 字,大约阅读时间需要 22 分钟。

修改 classes.html

{% extends "layout.html" %}{% block css %}{% endblock %}{% block content %}    

班级管理

{% for item in cls_list %}
{% endfor %}
ID 标题
{
{ item.id }}
{
{ item.caption }}
编辑| 跳转编辑|删除
{% endblock %}{% block js %} {% endblock %}

修改 urls.py

from django.contrib import adminfrom django.urls import pathfrom app01 import viewsurlpatterns = [    path('admin/', admin.site.urls),    path('login.html', views.Login.as_view()),    path('index.html', views.index),    path('classes.html', views.handle_classes),    path('add_classes.html', views.handle_add_classes),    path('edit_classes.html', views.handle_edit_classes),    path('student.html', views.handle_student),    path('teacher.html', views.handle_teacher),]

修改 views.py

from django.shortcuts import render, redirect, HttpResponse# Create your views here.from app01 import modelsfrom django import viewsfrom django.utils.decorators import method_decoratordef outer(func):    def inner(request, *args, **kwargs):        print(request.method)        return func(request, *args, **kwargs)    return innerclass Login(views.View):    def dispatch(self, request, *args, **kwargs):        # print(11111111111)        ret = super(Login, self).dispatch(request, *args, **kwargs)        # print(22222222222)        return ret    def get(self, request, *args, **kwargs):        print('GET')        return render(request, 'login.html', {'msg': ''})    def post(self, request, *args, **kwargs):        print('POST')        user = request.POST.get('user')        pwd = request.POST.get('pwd')        c = models.Administrator.objects.filter(username=user, password=pwd).count()        if c:            request.session['is_login'] = True            request.session['username'] = user            rep = redirect('/index.html')            return rep        else:            message = "用户名或密码错误"            return render(request, 'login.html', {'msg': message})def auth(func):    def inner(request, *args, **kwargs):        is_login = request.session.get('is_login')        if is_login:            return func(request, *args, **kwargs)        else:            return redirect('/login.html')    return innerdef index(request):    current_username = request.session.get('username')    if current_username:        return render(request, 'index.html', {'username': current_username})    else:        return redirect('/login.html')@authdef handle_classes(request):    if request.method == "GET":        current_user = request.session.get('username')        # 获取所有的班级列表        # for l in range(1, 102):        #     models.Classes.objects.create(caption='全栈一班%i' %(l))        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', 10)        pager = obj.pager_str()        cls_list = models.Classes.objects.all()[obj.db_start:obj.db_end]        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)        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)        # 判断是更新或者是添加操作        nid = request.POST.get('id', None)        # 更新操作        if nid and caption:            obj = models.Classes.objects.filter(id=nid).update(caption=caption)            # print(nid)            # print(caption)            response_dict['data'] = {'id': nid, 'caption': caption}        # 添加操作        elif caption:            obj = models.Classes.objects.create(caption=caption)            # print(obj.id)            # print(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))@authdef handle_add_classes(request):    message = ""    if request.method == 'GET':        return render(request, 'add_classes.html', {'msg': message})    elif request.method == 'POST':        caption = request.POST.get('caption')        if caption:            models.Classes.objects.create(caption=caption)        else:            message = '标题不能为空'            return render(request, 'add_classes.html', {'msg': message})        return redirect('/classes.html')    else:        return redirect('/index.html')# 通过url进行更新操作@authdef handle_edit_classes(request):    if request.method == "GET":        nid = request.GET.get('nid')        obj = models.Classes.objects.filter(id=nid).first()        return render(request, 'edit_classes.html', {'obj': obj})    elif request.method == "POST":        nid = request.POST.get('nid')        caption = request.POST.get('caption')        models.Classes.objects.filter(id=nid).update(caption=caption)        return redirect('/classes.html')    else:        return redirect('/index.html')def handle_teacher(request):    is_login = request.session.get('is_login')    if is_login:        current_user = request.session.get('username')        return render(request, 'teacher.html', {'username':current_user})    else:        return redirect('/login.html')def handle_student(request):    is_login = request.session.get('is_login')    if is_login:        current_user = request.session.get('username')        return render(request, 'student.html', {'username':current_user})    else:        return redirect('/login.html')

在 templates 文件夹下添加 edit_classes.html

{% extends "layout.html" %}{% block css %}{% endblock %}{% block content %}    

编辑班级

{% endblock %}{% block js %} {% endblock %}

通过 ajax 方式来更新操作

1334255-20190702141954577-497919312.png

通过 url 的方式来更新操作

1334255-20190702142017696-661758781.png

转载于:https://www.cnblogs.com/klvchen/p/11115621.html

你可能感兴趣的文章
Linux第七周学习总结——可执行程序的装载
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
细说php(二) 变量和常量
查看>>
iOS开发网络篇之Web Service和XML数据解析
查看>>
个人寒假作业项目《印象笔记》第一天
查看>>
java 常用命令
查看>>
ZOJ 1666 G-Square Coins
查看>>
CodeForces Round #545 Div.2
查看>>
卷积中的参数
查看>>
Linux中Zabbix4.0的搭建
查看>>
《LoadRunner没有告诉你的》之六——获取有效的性能需求
查看>>
51nod1076 (边双连通)
查看>>
Item 9: Avoid Conversion Operators in Your APIs(Effective C#)
查看>>
js去除空格
查看>>
学习Spring Boot:(二十八)Spring Security 权限认证
查看>>
IT学习神器——慕课网App获App Store、Android应用市场重磅推荐
查看>>
Linux网络状态工具ss命令使用详解
查看>>
深入浅出JavaScript(2)—ECMAScript
查看>>
编程珠玑第十一章----排序
查看>>
Face The Right Way POJ - 3276 (开关问题)
查看>>