博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
JSON跨域解决方案收集
查看>>
SSH框架整合总结
查看>>
图的深度优先遍历
查看>>
C# 之 提高WebService性能大数据量网络传输处理
查看>>
md5sum命令详解
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
应该是实例化对象的没有对属性赋值时,自动赋值为null,但不是空指针对象引用...
查看>>
原生HttpClient详细使用示例
查看>>
几道面试题
查看>>
Factory Design Pattern
查看>>
python中贪婪与非贪婪
查看>>
guava API整理
查看>>
无锁编程笔记
查看>>
jquery mobile
查看>>
如何在vue单页应用中使用百度地图
查看>>
Springboot使用步骤
查看>>
Spring属性注入
查看>>
Springboot-配置文件
查看>>
Springboot-日志框架
查看>>
SpringBoot-thymeleaf
查看>>