⽤python开发合同管理系统_python3.6+django2.0⼀⼩时学会
开发⼀套学。。。
1.在pycharm中新建project demo1 添加app01 点击create按钮完成新建
2.在demo项⽬⽬录下新建⽬录static,并在settings.py中追加代码:
STATICFILES_DIRS=(os.path.join(BASE_DIR, 'static'),)
3.在setting.py中添加模板路径:
TEMPLATES = [
{
'BACKEND': '...',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': ...,
'OPTIONS': {
'context_processors': [
...
],
},
},
]
4.学员管理系统数据库设计:
在app01/model.py⽬录下建⽴ 班级、⽼师、学⽣ 、⽼师与班级关联表 四张表:
from django.db import models
# Create your models here.
class Classes(models.Model):
'''
班级表
'''
title=models.CharField(max_length=32)
a=models.ManyToManyField('Teachers')
class Teachers(models.Model):
'''
⽼师表
'''
name=models.CharField(max_length=32)
class Students(models.Model):
username=models.CharField(max_length=32)
age=models.IntegerField()
gender=models.BooleanField()
cs=models.ForeignKey(Classes,on_delete=models.CASCADE)
在终端Terminal 项⽬⽬录下执⾏数据表更新命令:
python manage.py makemigrations
python manage.py migrate
⾄此⽣成了四张数据表,可以在pycharm中,点开右上⾓的Database⾯板,然后将项⽬中templates⽬录下边的db.sqlite3⿏标拖拽到Database⾯板下,对新创建的数据表进⾏查看。
5.学员管理系统之班级管理:
为了⽅便分别操作班级、⽼师、学⽣相关的业务,将app01⽬录下的views.py 删掉,在app01⽬录下新建⽬录views,并在views⽬录下新建classes.py teachers.py students.py。
django怎么学1.在classes.py 中写 get_classes add_classes del_classes edit_classes四个函数,完成对 班级数据 的增删改查:
from django.shortcuts import render,redirect
from app01 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):
hod=='GET':
return render(request,'add_classes.html')
hod=='POST':
title=('title','')
models.ate(title=title)
return redirect('/classes.html')
def del_classes(request):
nid=('nid','')
models.Classes.objects.filter(id=nid).delete()
return redirect('/classes.html')
def edit_classes(request):
hod=="GET":
nid = ('nid', '')
obj=models.(id=nid)
return render(request,'edit_classes.html',{'obj':obj})
hod=="POST":
nid=('nid','')
title=('xxoo','')
models.Classes.objects.filter(id=nid).update(title=title)
return redirect('/classes.html')
2.在urls.py 中配置url路由:
ib import admin
from django.urls import path
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', _classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
# path('teachers.html', _teachers),
# path('students.html', _studernts),
]
3.在template⽬录下建⽴所需的html页⾯⽂件:
get_classes.html
DOCTYPE html>
tr td{ border:1px solid #000;text-align:center;}
Title
ID 名称 操作
{% for row in cls_list %}
{{ row.id }}{{ row.title }}
|
{% endfor %}
add_classes.html
Title
{% csrf_token %}
edit_classes.html
Title
{% csrf_token %}
6.学员管理系统之学员管理:
1.在students.py 中写 get_students add_students del_students edit_students 四个函数,完成对 学⽣数据 的增删改查:
from django.shortcuts import render,redirect
from app01 import models
def get_students(request):
stu_list=models.Students.objects.all()
return render(request,'get_students.html',{'stu_list':stu_list})
def add_students(request):
hod=='GET':
cs_list=models.Classes.objects.all()
return render(request,'add_students.html',{'cs_list':cs_list})
hod=='POST':
u=('username','')
a=('age','')
g=('gender','')
c=('cs','')
models.ate(
username=u,
age=a,
gender=g,
cs_id=c
)
return redirect('/students.html')
def del_students(request):
nid = ('nid', '')
models.Students.objects.filter(id=nid).delete()
return redirect('/students.html')
def edit_students(request):
hod=="GET":
nid = ('nid', '')
obj=models.(id=nid)
cs_list = models.Classes.objects.all()
return render(request,'edit_students.html',{'obj':obj,'cs_list':cs_list}) hod=="POST":
nid=('nid','')
u = ('username', '')
a = ('age', '')
g = ('gender', '')
c = ('cs', '')
models.Students.objects.filter(id=nid).update(
username=u,
age=a,
gender=g,
cs_id=c)
return redirect('/students.html')
2.在urls.py 中配置url路由:
ib import admin
from django.urls import path
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', _classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
path('students.html', _students),
path('add_students.html', students.add_students),
path('del_students.html', students.del_students),
path('edit_students.html', students.edit_students),
# path('teachers.html', _teachers),
]
3.在template⽬录下建⽴所需的html页⾯⽂件:
get_students.html
tr td{ border:1px solid #000;text-align:center;}
Title
ID姓名年龄性别班级操作
{% for row in stu_list %}
{{ row.id }}{{ row.username }}{{ row.age }}{{ der }}{{ row.cs.title }} |
{% endfor %}
add_students
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论