• 数据库表的增删改查学生信息管理


    使用post方法,csrf必须注释掉

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        #'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]

    models

    from django.db import models
    
    # Create your models here.
    class Classes(models.Model):#班级表
        title=models.CharField(max_length=32)
        m=models.ManyToManyField('Teacheres')#加上引号,全执行完后才来执行,表的多对多
    
    class Teacheres(models.Model):#教师表
        name=models.CharField(max_length=32)
    
    class Student(models.Model):
        username=models.CharField(max_length=32)
        age=models.IntegerField()
        #gender=models.BooleanField()布尔值
        gender = models.NullBooleanField()#可以为空的布尔值
        cs=models.ForeignKey(Classes)#这个classes不用加引号,因为在上面,顺序执行

    urls,^这个必须加,记住

    from django.conf.urls import url
    from django.contrib import admin
    from app.views import classes
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^classes.html$',classes.get_classes),#不加^访问点击页面没反应
        url(r'^add_classes.html$',classes.add_classes),
        url(r'^del_classes.html$', classes.del_classes),
        url(r'^edit_classes.html$',classes.edit_classes),
    
    ]

    views.py删除,新建文件夹,views里面建classes.py文件,内容如下

    from django.shortcuts import render
    from django.shortcuts import redirect
    from app import models
    def get_classes(request):
    
        cls_list=models.Classes.objects.all()
    
        return render(request,'get_classes.html',{'cls_list':cls_list})
    
    def add_classes(request):
        if request.method == 'GET':
            return render(request, 'add_classes.html')
        elif request.method == 'POST':
            title1=request.POST.get("title")
            models.Classes.objects.create(title=title1)
            return redirect('/classes.html')
             #url路径不是html文件
    
    def del_classes(request):
        nid=request.GET.get("nid")
        models.Classes.objects.filter(id=nid).delete()
        return redirect("/classes.html")
    
    def 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.GET.get('nid')
            title=request.POST.get('abc')
            models.Classes.objects.filter(id=nid).update(title=title)
            return redirect("/classes.html")

    get_classes.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div><a href="/add_classes.html">添加</a></div>
    <div> <table border="1">
        <thead>
        <tr>
            <th>ID</th>
            <th>名称</th>
            <th>操作</th>
        </tr>
    </thead>
        <tbody>
        {% for row in cls_list %}
            <tr>
            <td>{{ row.id }}</td>
            <td>{{ row.title }}</td>
            <td><a href="del_classes.html?nid={{ row.id }}">删除</a>
                |
            <a href="edit_classes.html?nid={{ row.id }}">修改</a>
            </td>
            </tr>
        {% endfor %}
        </tbody>
    </table></div>
    </body>
    </html>

    add_class.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="add_classes.html" method="POST">
        {% csrf_token %}<!--提交数据-->
        <input type="text"name="title">
        <input type="submit"value="提交">
    </form>
    </body>
    </html>

    edit_classes.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="edit_classes.html?nid={{ obj.id }}"method="POST">
        {% csrf_token %}
    <input type="text"name="abc"value="{{ obj.title }}">
    <input type="submit"value="提交"></form>
    </body>
    </html>
  • 相关阅读:
    弱口令扫描.python脚本
    扫描web目录的Python小脚本
    Linux /etc目录重要文件
    linux(centos7)系统优化
    泛型程序设计
    对象包装器与自动装箱
    打包apk时,提示:error: Error: Resource is not public.
    SpringMVC分页实现
    IDEA搭建基于maven 的SSM框架
    ssm框架搭建
  • 原文地址:https://www.cnblogs.com/wfl9310/p/9438884.html
Copyright © 2020-2023  润新知