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