zoukankan      html  css  js  c++  java
  • Python学习第150天(目前正在做的内容介绍)

    目前正在按照相关教程制作一个具有班级、学生、老师三者之间相互关系的网页操作。目前暂时完成学生和班级部分内容,最为关键的manytomany班级与老师之间的关系还没有完善,这个小例子对实际操作的过程使用非常有帮助,以下是近期代码内容:

    model.py

    from django.db import models
    
    class Classes(models.Model):
        # 班级表
        titile = models.CharField(max_length=32)
        m = models.ManyToManyField('Teachers')
    
    class Teachers(models.Model):
        # 老师表
        name = models.CharField(max_length=36)
    
    class Students(models.Model):
        username = models.CharField(max_length=32)
        age = models.IntegerField()
        gender = models.BooleanField()
        cs = models.ForeignKey(Classes, on_delete=None)

    url.py

    from django.contrib import admin
    from django.urls import path,re_path
    from app01.views import classes
    from app01.views import students
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        re_path('^classes.html$',classes.get_classes),
        re_path('^add_classes.html$',classes.add_classes),
        re_path('^del_classes.html$',classes.del_classes),
        re_path('^edit_classes.html$',classes.edit_classes),
    
        re_path('^students.html$',students.get_students),
        re_path('^add_students.html$',students.add_studens),
        re_path('^del_students.html$',students.del_students),
        re_path('^edit_students.html$',students.edit_students)
    
        # re_path('^set_teacher.html$',classes.set_teacher),
        # path('a',classes.a_test),
        # path('b',classes.b_test),
    ]

    classes.py

    from django.shortcuts import render
    from django.shortcuts import redirect
    from app01 import models
    
    def get_classes(req):
        cls_list = models.Classes.objects.all()
        for item in cls_list:
            print(item.id,item.titile,item.m.all())
        return render(req,'get_classes.html',{'cls_list':cls_list})
    
    def add_classes(req):
        if req.method == 'GET':
            return render(req,'add_classes.html')
        elif req.method == 'POST':
            title = req.POST.get('title')
            models.Classes.objects.create(titile = title)
            return redirect('/classes.html')
    
    def del_classes(req):
        nid = req.GET.get('nid')
        print(nid)
        models.Classes.objects.filter(id=nid).delete()
        return redirect('/classes.html')
    
    def edit_classes(req):
        if req.method == 'GET':
            nid = req.GET.get('nid')
            obj = models.Classes.objects.filter(id=nid).first()
            return render(req,'edit_classes.html',{'obj':obj})
        elif req.method == 'POST':
            nid = req.GET.get('nid')
            title = req.POST.get('xoxo')
            models.Classes.objects.filter(id=nid).update(titile = title)
            return redirect('/classes.html')

    students.py

    from django.shortcuts import render,redirect
    from app01 import models
    
    def get_students(req):
        stu_list = models.Students.objects.all()
        return render(req,'get_students.html',{'stu_list':stu_list})
    
    def add_studens(req):
        if req.method == 'GET':
            cs_list = models.Classes.objects.all()
            return render(req,'add_students.html',{'cs_list':cs_list})
        elif req.method == 'POST':
            u = req.POST.get('username')
            a = req.POST.get('age')
            g = req.POST.get('gender')
            c = req.POST.get('cs')
            print(u,a,g,c)
            models.Students.objects.create(
                username=u,
                age=a,
                gender=g,
                cs_id=c
            )
            return redirect('/students.html')
    
    def del_students(req):
        nid = req.GET.get('nid')
        models.Students.objects.filter(id=nid).delete()
        return redirect('/students.html')
    
    def edit_students(req):
        if req.method == 'GET':
            nid = req.GET.get('nid')
            obj = models.Students.objects.filter(id=nid).first()
            cs_list = models.Classes.objects.all()
            return render(req, 'edit_students.html', {'obj': obj,'cs_list':cs_list})
        elif req.method == 'POST':
            nid = req.GET.get('nid')
            cs_list = models.Classes.objects.all()
            # title = req.POST.get('xoxo')
            u = req.POST.get('username')
            a = req.POST.get('age')
            g = req.POST.get('gender')
            c = req.POST.get('c')
            models.Students.objects.filter(id=nid).update(
                username=u,
                age=a,
                gender=g,
                cs_id=c
            )
            return redirect('/students.html')

    其中最为关键的一个视图模板

    edit_students.html的内容

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="edit_students.html?nid={{ obj.id }}" method="POST">
            {% csrf_token %}
    {#        <p style="display: none"> <input type="text" name="id" value="{{ obj.id }}" /></p>#}
            <p> <input type="text" name="username" value="{{ obj.username }}" /></p>
            <p> <input type="text" name="age" value="{{ obj.age }}" /></p>
            <p>
                {% if obj.gender %}
                    男:<input type="radio" name="gender" checked="checked" value="1" />
                    女:<input type="radio" name="gender" value="0" />
                    {% else %}
                    男:<input type="radio" name="gender" value="1" />
                    女:<input type="radio" name="gender" value="0" checked="checked"  />
                {% endif %}
            </p>
            <p>
                <select name="c">
                    {% for row in cs_list %}
                        <option value="{{ row.id }}">{{ row.titile }}</option>
                    {% endfor %}
                </select>
            </p>
            <input type="submit" value="提交" />
        </form>
    
    </body>
    </html>

    目前已经完成了基本功能,完整功能还是等这个部分全部做完后进行详述。

    关键的几个注意点:

    1. 类代表数据库表
    2. 类的对象代指数据库的一行记录
    3.ForeignKey字段代指关联表中的一行数据(类的对象)

    4.
    - 正向:fk字段 (*****)
    - 反向:小写类名_set(默认) ==> related_name='ssss'

    5. 谁是主表?就全部列出其数据
    models.Student.objects.all().values('username', 'cs__titile')
    models.Classes.objects.all().values('titile', 'ssss__username')

    4. M2M字段,自动生成第三张表;依赖关联表对第三张表间接操作

    5.filter在查找的过程中涉及外键,可使用双下划线不断向下查询

    以上为目前暂时更新的内容。

  • 相关阅读:
    【spring cloud】spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient
    【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构
    【IntelliJ IDEA】idea显示工具栏
    笔记:git基本操作
    Spring Boot中使用AOP统一处理Web请求日志
    Spring Boot应用的后台运行配置
    RSA加密
    Java汉字md5值不一致问题
    如何判断一个请求是不是ajax请求
    SpringBoot拦截器中service或者redis注入为空的问题
  • 原文地址:https://www.cnblogs.com/xiaoyaotx/p/13485306.html
Copyright © 2011-2022 走看看