野外泛在线考核系统(五)


二、笔记本端
接(四),继续完成Django的相关配置

(五)Django 数据管理

主要目的是完成对数据库数据的管理

1.添加数据

  1. 首先要添加数据,在templates中添加addData.html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
<body>
  <h3>{{ title }}</h3>
  <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="确定">
  </form>
</body>
</html>
  1. 在Lesson中添加form.py
from django import forms
from .models import Lesson
class LessonInfoForm(forms.ModelForm):
  class Meta:
      model = Lesson
      fields = '__all__'
  1. 修改Lesson/urls.py
# 定义添加数据的路由
  path('addData', AddData.as_view(), name='addData'),   
  path('result', result, name='result'),
  1. 在Lesson/views.py中:
#定义添加数据的路由
from django.views.generic.edit import FormView,CreateView,UpdateView,DeleteView
from .form import LessonInfoForm
from .models import Lesson
from django.http import HttpResponse

def result(request):
  return HttpResponse('Success')
'''   
#这是需要form.py的方法
class addData(FormView):
  initial = {'lessonID': '001', 'lessonName': '行动方法课','lessonWeight': 73.6}
  template_name = 'addData.html'
  success_url = '/result'
  form_class = LessonInfoForm
  extra_context = {'title': '课程信息表'}

'''
#这是不需要form.py的方法
class AddData(CreateView):
  initial = {'lessonID': '001', 'lessonName': '行动方法课', 'lessonWeight': 73.6}
  template_name = 'addData.html'
  success_url = '/result'
  # 表单生成方式一
  # form_class = LessonInfoForm
  # 表单生成方式二
  model = Lesson
  # fields设置模型字段,从而生成表单字段
  fields = ['lessonID', 'lessonName', 'lessonWeight']
  extra_context = {'title': '课程信息表'}

2.修改数据

  1. 首先要添加数据,在templates中添加updateData.html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
<body>
  <h3>{{ title }}-{{ lessoninfo.lessonName }}</h3>
  <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="确定">
  </form>
</body>
</html>
  1. 修改Lesson/urls.py
# 定义添加数据的路由
 path('<lessonID>Update', UpdateData.as_view(), name='updateData'),
  path('result', result, name='result'),
  1. 在Lesson/views.py中:
from django.views.generic.edit import FormView,CreateView,UpdateView,DeleteView
from .form import LessonInfoForm
from .models import Lesson
from django.http import HttpResponse

def result(request):
  return HttpResponse('Success')

class UpdateData(UpdateView):
  template_name = 'updateData.html'
  success_url = '/result'
  model = Lesson
  # fields设置模型字段,从而生成表单字段
  fields = ['lessonID', 'lessonName', 'lessonWeight']
  slug_url_kwarg = 'lessonID'
  slug_field = 'lessonID'
  context_object_name = 'lessoninfo'
  extra_context = {'title': '课程信息表'}

3.删除数据

  1. 首先要添加数据,在templates中添加deleteData.html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
<body>
  <h3>{{ title }}-{{ lessoninfo.lessonName }}</h3>
  <form method="post">
      {% csrf_token %}
    <div>删除{{ lessoninfo.lessonName }}?</div>
      <input type="submit" value="确定">
  </form>
</body>
</html>
  1. 修改Lesson/urls.py
# 定义添加数据的路由
  path('<pk>Delete', DeleteData.as_view(), name='deleteData'),
  path('result', result, name='result'),
  1. 在Lesson/views.py中:
from django.views.generic.edit import FormView,CreateView,UpdateView,DeleteView
from .form import LessonInfoForm
from .models import Lesson
from django.http import HttpResponse

def result(request):
  return HttpResponse('Success')

class DeleteData(DeleteView):
  template_name = 'deleteData.html'
  success_url = '/result'
  model = Lesson
  # fields设置模型字段,从而生成表单字段
  context_object_name = 'lessoninfo'
  extra_context = {'title': '课程信息表'}

4.显示数据

  1. 首先要添加数据,在templates中添加listData.html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
<body>
  <h3>{{ title }}</h3>
  <table border="1">
    {% for i in lessoninfo %}
        <tr>
             <th>{{ i.id }}</th>
          <th>{{ i.lessonID }}</th>
          <th>{{ i.lessonName }}</th>
            <th>{{ i.lessonWeight }}</th>
        </tr>
    {% endfor %}
  </table>
  <br>
  {% if is_paginated %}
  <div class="pagination">
      <span class="page-links">
          {% if page_obj.has_previous %}
              <a href="?page={{ page_obj.previous_page_number }}">上一页</a>
          {% endif %}
          {% if page_obj.has_next %}
              <a href="?page={{ page_obj.next_page_number }}">下一页</a>
          {% endif %}
          <br>
          <br>
          <span class="page-current">
              第{{ page_obj.number }}页,共{{ page_obj.paginator.num_pages }}页。
          </span>
      </span>
  </div>
  {% endif %}
</body>
</html>
  1. 修改Lesson/urls.py
   # 定义显示数据的路由
  path('listData',ListData.as_view(), name='listData'),
  1. 在Lesson/views.py中:
#显示数据
from django.views.generic import ListView
from .models import Lesson
class ListData(ListView):
  # 设置模版文件
  template_name = 'listData.html'
  # 设置模型外的数据
  extra_context = {'title': '课程信息表'}
  # 查询模型PersonInfo
  queryset = Lesson.objects.all()
  # 每页的展示20条数据
  paginate_by = 5
  # 如不设置,则模版上下文默认为lessoninfo_list
  context_object_name = 'lessoninfo'

5.管理数据

  1. 首先要添加数据,在templates中添加manageData.html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
<body>
  <h3>{{ title }}</h3>
  <table border="1">
       <tr>
             <th>课程编号</th>
          <th>课程编号</th>
          <th>课程名称</th>
            <th>形成性考核权重</th>
            <th>管理</th>
        </tr>
    {% for i in lessoninfo %}

        <tr>
             <td>{{ i.id }}</td>
          <td>{{ i.lessonID }}</td>
          <td>{{ i.lessonName }}</td>
            <td>{{ i.lessonWeight }}</td>
            <td><a href="{{i.id}}Delete"><h3 >删除</h3></a></td>
        </tr>
    {% endfor %}
  </table>
  <br>
  {% if is_paginated %}
  <div class="pagination">
      <span class="page-links">
          {% if page_obj.has_previous %}
              <a href="?page={{ page_obj.previous_page_number }}">上一页</a>
          {% endif %}
          {% if page_obj.has_next %}
              <a href="?page={{ page_obj.next_page_number }}">下一页</a>
          {% endif %}
          <br>
          <br>
          <span class="page-current">
              第{{ page_obj.number }}页,共{{ page_obj.paginator.num_pages }}页。
          </span>
      </span>
  </div>
  {% endif %}
</body>
</html>
  1. 修改Lesson/urls.py
 # 定义管理数据的路由
  path('manageData', ManageData.as_view(), name='manageData'),
  1. 在Lesson/views.py中:
#数据管理
from django.views.generic import ListView
from .models import Lesson
class ManageData(ListView):
  # 设置模版文件
  template_name = 'manageData.html'
  # 设置模型外的数据
  extra_context = {'title': '课程信息表'}
  # 查询模型PersonInfo
  queryset = Lesson.objects.all()
  # 每页的展示20条数据
  paginate_by = 5
  # 如不设置,则模版上下文默认为lessoninfo_list
  context_object_name = 'lessoninfo'

6.管理界面

  1. 修改Lesson/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Django管理测试</title>
</head>
<body>
我的第一个页面

<a href="vue"><h3 >测试Vue</h3></a>
<br>
<a href="download"><h3 >下载测试</h3></a>
<br>
<a href="upload"><h3 >上传测试</h3></a>
<br>
<a href="listData"><h3 >显示课程</h3></a>
<br>
<a href="addData"><h3 >添加课程</h3></a>
<br>
<a href="manageData"><h3 >管理课程</h3></a>
{{hello}}

</body>
</html>

最终结果如下图所示:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值