python+django+sqlite3,不成熟的学生管理增删改查

这篇博客记录了作者使用Python的Django框架结合SQLite3数据库,逐步构建一个简单学生管理系统的过程。内容包括项目创建、应用设置、数据库模型定义、后台管理配置以及前端模板的搭建,但未完成登录验证和前端美化,目前仅实现了基本的CRUD操作。
摘要由CSDN通过智能技术生成

因为是边学边做的,所以有一些地方有了很麻烦的方法,登录验证也没做完,前端也很难看,只是做了基本的增删改查



1.新建project

django-admin.py startproject student

2.新建app

python manage. py startapp guanli

3.setting设置

INSTALLED_APPS = 

中添加新建的guanli     app

4.urls设置

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('guanli/', include(('guanli.urls','guanli'), namespace='guanli'))
]

5.进入guanli   app  下的models中添加数据库表

from django.db import models


class student(models.Model):
    xuehao = models.IntegerField(primary_key=True, verbose_name='学号')
    xingming = models.TextField(blank=True, null=True, verbose_name='姓名')
    xingbie = models.TextField(blank=True, null=True)
    age = models.IntegerField(blank=True, null=True, verbose_name='年龄')
    csrq = models.DateField(blank=True, null=True)
    bjbh = models.IntegerField(blank=True, null=True)
    phone = models.TextField(blank=True, null=True)
    sfzh = models.IntegerField(blank=True, null=True)

    def __int__(self):
        return self.xuehao


class teacher(models.Model):
    bianhao = models.IntegerField(primary_key=True)
    xingming = models.CharField(max_length=32)
    xingbie = models.TextField()
    csrq = models.DateField()
    zzmm = models.TextField()
    phone = models.IntegerField()
    sfzh = models.IntegerField()


class login(models.Model):
    user = models.TextField()
    password = models.TextField()
5.使用
1 python manage.py makemigrations
2 python manage.py migrate
 

将数据库信息添加到sqlite3中,因为是内置的所以不需要设置api

6.在guanli下的admin中添加

from django.contrib import admin
from .models import student
from .models import teacher
from .models import login


class ss(admin.ModelAdmin):
    list_display = ('xuehao', 'xingming', 'age')  # 页面显示


# list_editable = ('xingming','age')     #直接修改
# filter_horizontal = ('age',)
# list_per_page = 2       #分页
# search_fields = ('xuehao','xingming')      #查询
# list_filter = ('age','xuehao')             #辅助过滤查询
admin.site.register(student, ss)
admin.site.register(teacher)
admin.site.register(login)

在guanli 的apps中

from django.apps import AppConfig


class GuanliConfig(AppConfig):
    name = 'guanli'

7.在guanlixia的urls中


from django.urls import path
from. import views
app_name='guanli'

urlpatterns = [
    path('index/', views.index),
    path('sms/', views.stus),
    path('gon/', views.gon,name='gon'),
    path('add/', views.add,name='add'),
    path('query/', views.query,name='query'),
    path('change/', views.change,name='change'),
    path('delete/', views.delete,name='delete'),
   path('cxjg/', views.cxjg,name='cxjg'),
    path('change1/', views.change1,name='change1'),
    path('xiugai/', views.xiugai,name='xiugai'),
    path('shanchu/', views.shanchu,name='shanchu'),
    path('login/', views.login,name='login'),
    path('dele/', views.dele,name='dele'),
    path('logi/', views.logi,name='logi'),
]
8.在guanli 下的views中
from django.shortcuts import render
from django.http import HttpResponse
from .import models

def index(request):
   return render(request,'ss/index.html')

def stus(request):
   stus=models.student.objects.all()
   return render(request,'ss/stus.html',{
 'stus':stus})

def gon(request):
   return render(request,'ss/gon.html')
def add(request):
   xh = request.POST.get('xuehao')
   xm = request.POST.get('xingming')
   xingbie = request.POST.get('xingbie')
   age = request.POST.get('age')
   csrq = request.POST.get('csrq')
   bjbh = request.POST.get('bjbh')
   phone = request.POST.get('phone')
   sfzh = request.POST.get('sfzh')
   # bc = models.student()
   # bc.xuehao = xh
   # bc.xingming=xm
   # bc.xingbie=xingbie
   # bc.age=age
   # bc.csrq=csrq
   # bc.bjbh=bjbh
   # bc.phone=phone
   # bc.sfzh=sfzh
   # bc.save()
   try:
      if (xm):
         models.student.objects.create(xuehao=xh, xingming=xm, xingbie=xingbie, age=age, csrq=csrq, bjbh=bjbh,
                                   phone=phone, sfzh=sfzh)
      ret
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值