1.工程目录

2.urls.py

"""Django_ajax URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views urlpatterns = [
path('admin/', admin.site.urls), path('students/', views.students),
path('add_student/', views.add_student),
path('del_student/', views.del_student),
path('edit_student/', views.edit_student),
path('edit_student/', views.edit_student), ]

3.views.py

from django.shortcuts import render, HttpResponse, redirect
from app01 import models
import json
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # Create your views here. class CustomPaginator(Paginator):
def __init__(self, current_page, show_page_num, *args, **kwargs):
self.current_page = current_page
self.show_page_num = show_page_num
super(CustomPaginator, self).__init__(*args, **kwargs)
print('current_page:', current_page)
print('show_page_num:', show_page_num) def pager_num_range(self): if self.current_page-self.show_page_num > 0 and self.current_page+self.show_page_num <= self.num_pages:
limit_left = int(self.current_page-self.show_page_num/2)
limit_right = int(self.current_page + self.show_page_num/2+1)
elif self.current_page-self.show_page_num <= 0:
limit_left = 1
limit_right = 1 + self.show_page_num+1
elif self.current_page+self.show_page_num > self.num_pages:
limit_right = self.num_pages+1
limit_left = self.num_pages-self.show_page_num return range(limit_left, limit_right) def students(request): current_page = request.GET.get('p')
num_A_page = request.GET.get('n')
show_page_num = 10 if num_A_page == None:
num_A_page = 10 paginator = CustomPaginator(int(current_page), show_page_num, models.Student.objects.all(), num_A_page)
try:
posts = paginator.page(current_page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages) cls_list = models.Classes.objects.all()
return render(request, 'students.html', {'posts': posts, 'cls_list': cls_list}) def add_student(request):
print(request.POST)
response = {'status': True, 'message': None, 'data': None}
try: u = request.POST.get('stu_username')
a = request.POST.get('stu_age')
g = request.POST.get('gender')
c = request.POST.get('cls_id')
print('-------------', u, a, g, c)
obj = models.Student.objects.create(username=u, age=a, gender=g, cs_id=c)
response['data'] = obj.id except Exception as e:
response['status'] = False
response['message'] = '用户输入错误!' result = json.dumps(response, ensure_ascii=False)
return HttpResponse(result) def del_student(request):
response = {'status': True, 'message': None, 'data': None} nid = request.GET.get('nid')
models.Student.objects.filter(id=nid).delete() result = json.dumps(response, ensure_ascii=False) return HttpResponse(result) def edit_student(request):
print(request.POST)
response = {'status': True, 'message': None, 'code': 1000}
try:
nid = request.POST.get('nid')
u = request.POST.get('user')
a = request.POST.get('age')
g = request.POST.get('gender')
c = request.POST.get('edit_cls_id')
print('-------------', u, a, g, c) models.Student.objects.filter(id=nid).update(username=u, age=a, gender=g, cs_id=c) except Exception as e:
response['status'] = False
response['code'] = 1001
response['message'] = '用户输入错误!' import json
result = json.dumps(response, ensure_ascii=False) return HttpResponse(result)

4.models.py

from django.db import models

# Create your models here.

class Classes(models.Model):
title = models.CharField(max_length=32)
m = models.ManyToManyField('Teachers') class Teachers(models.Model):
name = models.CharField(max_length=32) class Student(models.Model):
username = models.CharField(max_length=32)
age = models.IntegerField()
gender = models.BooleanField()
cs = models.ForeignKey(Classes, on_delete=models.CASCADE)

5.在static文件夹中加入jquery-3.3.1.js、创建plugins文件夹,在该文件夹下加入要使用是前端框架。

链接:https://pan.baidu.com/s/1PrYlNmW3FyOlvkQhb7KYiQ
提取码:1cc9
6.生成数据库中的表

在pycharm中的Terminal中输入:

python manage.py makemigrations

python manage.py migrate

7.运行服务器:

在pycharm中的Terminal中输入:

python manage.py runserver 8081

运行成功

8.访问效果:

Django,ajax实现表格增删查改,Django内置分页功能。的更多相关文章

  1. django models进行数据库增删查改

    在cmd 上运行 python manage.py shell   引入models的定义 from app.models import  myclass   ##先打这一行    ------这些是 ...

  2. ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页!

    转自:http://www.cnblogs.com/lt-style/p/3457399.html 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号.班级名称 开发过程: 1. ...

  3. JAVAWEB实现增删查改(图书信息管理)之修改功能实现

    首先通过点击index.jsp页面的修改按钮,获取该行的id:↓ 其次,跳转到updateBooks.jsp页面进行修改信息,页面代码如下:↓ <%@ page import="Boo ...

  4. JAVAWEB实现增删查改(图书信息管理)之添加功能实现

    addBooks.jsp页面代码:↓ <%-- Created by IntelliJ IDEA. User: NFS Date: 2019-7-12 Time: 14:30 To change ...

  5. JavaWeb实现增删查改(图书信息管理)之删除功能实现

    —————————————————————————————————————————————————————————— 删除按钮对应的servlet -->DeleteBooks.java  ↓ ...

  6. JDBC课程4--使用PreparedStatement进行增删查改--封装进JDBCTools的功能中;模拟SQL注入 ; sql的date()传入参数值格式!

    主要内容: /*SQL 的date()需要传入参数值: preparedStatement().setDate(new java.util.Date().getTime()); 熟悉了使用Prepar ...

  7. Java实现单链表的增删查改及逆置打印

    //所提供的接口 LinkList.java package Struct; public interface LinkList {//判断链表为空public boolean linkListIsE ...

  8. django内置分页功能扩展

    实现自定制页码数类型class myPaginator(Paginator): def __init__(self,curr_page,per_page_num,*args,**kwargs): se ...

  9. Django笔记&教程 5-1 基础增删查改

    Django 自学笔记兼学习教程第5章第1节--基础增删查改 点击查看教程总目录 第四章介绍了模型类models.Model和创建模型,相当于介绍了数据库表和如何创建数据库表. 这一章将介绍如何使用模 ...

随机推荐

  1. Android跳转淘宝、京东APP商品详情页

    import Android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; imp ...

  2. Linux之解决命令行cat命令中文乱码

    临时解决cat中文乱码 cat test.txt | iconv -f GBK -t UTF-8

  3. leetcode-59-螺旋矩阵 II

    题目描述: 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ ...

  4. Python全栈开发:list、元祖常用方法操作

    列表[] 索引与切片#例题#li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong'] # #例子1 找出列表中索引为0的元 ...

  5. Sequential Minimal Optimization(SMO,序列最小优化算法)初探

    什么是SVM SVM是Support Vector Machine(支持向量机)的英文缩写,是上世纪九十年代兴起的一种机器学习算法,在目前神经网络大行其道的情况下依然保持着生命力.有人说现在是神经网络 ...

  6. 搭建django环境

    一.安装django(两种方式) 1.pip install django 2.python setup.py install(下载gjango包:https://www.djangoproject. ...

  7. pycharm使用github

    pycharm使用github 绑定账号 File-settings 在搜索框输入git 会出现github,然后在旁边输入你github的用户名和密码,可以点击”test”测试一下,如果出现: Co ...

  8. FX4300超频4.7GHz

    先贴出本人计算机配置: (本人cpu为fx4300,默认频率为3.8GHz) 谨记:超频有风险,很可能烧坏主板.cpu.内存等硬件,特别是增加主板电压时一定一次增加0.025V,不要一次增加太多,并且 ...

  9. PHP之string

    string addcslashes() Quote string with slashes in a C style 以 C 语言风格使用反斜线转义字符串中的字符 addslashes() Quot ...

  10. html中设置textbox的宽和高

    1.宽:width是不行的,而应该用size size=30,表示能输入30个字符 2.高:  style="height:50px"