Python3.6 + django2.0 developed a student management system, python3.6django2.0

Source: Internet
Author: User

Python3.6 + django2.0 developed a student management system, python3.6django2.0

1. create project demo1 add app01 in pycharm and click create.

2. Create a directory static under the demo project directory and append the code to settings. py:

STATICFILES_DIRS=(os.path.join(BASE_DIR, 'static'),)

3. Add the template path to setting. py:

TEMPLATES = [ {  'BACKEND': '...',  'DIRS': [os.path.join(BASE_DIR, 'templates'),],  'APP_DIRS': ...,  'OPTIONS': {   'context_processors': [    ...   ],  }, },]

4. Student Management System Database Design:

In the app01/model. py directory, create four tables of association between class, teacher, student, and teacher and class:

From django. db import models # Create your models here. class Classes (models. model): '''class table''' title = models. charField (max_length = 32) a = models. manyToManyField ('teachers') class Teachers (models. model): ''' instructor table ''' 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)

Run the data table update command in the Terminal project directory:

python manage.py makemigrationspython manage.py migrate

At this point, four data tables are generated. You can open the Database panel in the upper right corner in pycharm, and then store the db under the templates directory in the project. drag sqlite3 to the Database panel to view the newly created data table.

5. Class Management in the student management system:

To facilitate the operations on the business related to classes, teachers, and students separately. delete py, create the directory views under the app01 directory, and create a new classes under the views directory. py teachers. py students. py.

1. In classes. py, write the get_classes add_classes del_classes edit_classes function to add, delete, modify, and query the class data:

from django.shortcuts import render,redirectfrom app01 import modelsdef get_classes(request): cls_list = models.Classes.objects.all() return render(request,'get_classes.html',{'cls_list':cls_list})def add_classes(request): if request.method=='GET':  return render(request,'add_classes.html') elif request.method=='POST':  title=request.POST.get('title','')  models.Classes.objects.create(title=title)  return redirect('/classes.html')def del_classes(request): nid=request.GET.get('nid','') models.Classes.objects.filter(id=nid).delete() return redirect('/classes.html')def edit_classes(request): if request.method=="GET":  nid = request.GET.get('nid', '')  obj=models.Classes.objects.get(id=nid)  return render(request,'edit_classes.html',{'obj':obj}) elif request.method=="POST":  nid=request.POST.get('nid','')  title=request.POST.get('xxoo','')  models.Classes.objects.filter(id=nid).update(title=title)  return redirect('/classes.html')

2. Configure url routing in urls. py:

from django.contrib import adminfrom django.urls import pathfrom app01.views import classes,students,teachersurlpatterns = [ path('admin/', admin.site.urls), path('classes.html', classes.get_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.get_teachers), # path('students.html', students.get_studernts),]

3. create the required html page file under the template directory:

Get_classes.html

DOCTYPE html> 

Add_classes.html

<! DOCTYPE html> 

Edit_classes.html

<! DOCTYPE html> 

6. Student Management in the student management system: 1. Write the get_students add_students del_students edit_students functions in students. py to complete the addition, deletion, modification, and query of student data:

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

2. Configure url routing in urls. py:

from django.contrib import adminfrom django.urls import pathfrom app01.views import classes,students,teachersurlpatterns = [ path('admin/', admin.site.urls), path('classes.html', classes.get_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.get_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.get_teachers),]

3. create the required html page file under the template directory:

Get_students.html

<! DOCTYPE html> 

Add_students

<! DOCTYPE html> 

Edit_students.html

<! DOCTYPE html> 

7. Assign teachers to the class through the student management system:

Add some instructor information to the teachers data table:

Open the Database panel in the upper-right corner of pycharm, drag the db. splte3 under the template directory to the Database panel, and open the db = "app01_teachers table

Click "+", enter the instructor information, and click the up arrow with the "DB" icon to save the data.

1. Add the set_teachers function in classes. py.

def set_teachers(request): if request.method=='GET':  nid=request.GET.get('nid','')  cls_obj=models.Classes.objects.get(id=nid)  cls_teacher_list=cls_obj.a.all()  all_teacher_list=models.Teachers.objects.all()  return render(request,'set_teachers.html',{   'cls_teacher_list':cls_teacher_list,   'all_teacher_list':all_teacher_list,   'nid':nid,  }) elif request.method=='POST':  nid = request.POST.get('nid', '')  ids_str=request.POST.getlist('teacher_id','')  ids_int=[]  for i in ids_str:   i=int(i)   ids_int.append(i)  obj=models.Classes.objects.get(id=nid)  obj.a.set(ids_int)  return redirect('/classes.html')

2. Configure url routing in urls. py:

from django.contrib import adminfrom django.urls import pathfrom app01.views import classes,students,teachersurlpatterns = [ path('admin/', admin.site.urls), path('classes.html', classes.get_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.get_students), path('add_students.html', students.add_students), path('del_students.html', students.del_students), path('edit_students.html', students.edit_students), path('set_teachers.html', classes.set_teachers),]
 

3. create the required html page file under the template directory:

Set_teachers.html

<! DOCTYPE html> 

Add and modify get_classes.html:

<! DOCTYPE html> 

8. Get started with Ajax

Ajax is an asynchronous transmission method, which secretly sends requests to the backend without page refreshing. The following is a small example to learn about Ajax as a data transmission method.

First download the static directory under the jQuery import Project

1. Create ajax. py in the app01/Views directory

From django. shortcuts import render, redirect, HttpResponsedef ajax1 (request): return render(request,'ajax1.html ') def ajax2 (request): u = request. GET. get ('username') p = request. GET. get ('Password') return HttpResponse ('I prefer ')

2. Configure related routes in urls. py

from django.contrib import adminfrom django.urls import pathfrom app01.views import classes,students,teachers,ajaxurlpatterns = [ path('admin/', admin.site.urls), path('classes.html', classes.get_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.get_students), path('add_students.html', students.add_students), path('del_students.html', students.del_students), path('edit_students.html', students.edit_students), path('set_teachers.html', classes.set_teachers), path('ajax1.html', ajax.ajax1), path('ajax2.html', ajax.ajax2),]

3.create ajax1.html in the templatedirectory

<! DOCTYPE html> 

9. Ajax deletion of student management system: 1. Add the ajax4 function to ajax. py.

From app01 import modelsdef ajax4 (request): nid = request. GET. get ('nid') msg = 'successful' try: models. students. objects. get (id = nid ). delete () failed t Exception as e: msg = str (e) return HttpResponse (msg)

2. Configure related routes in urls. py

from django.contrib import adminfrom django.urls import pathfrom app01.views import classes,students,teachers,ajaxurlpatterns = [ path('admin/', admin.site.urls), path('classes.html', classes.get_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.get_students), path('add_students.html', students.add_students), path('del_students.html', students.del_students), path('edit_students.html', students.edit_students), path('set_teachers.html', classes.set_teachers), path('ajax1.html', ajax.ajax1), path('ajax2.html', ajax.ajax2), path('ajax4.html', ajax.ajax4),]

3.add and modify get_students.html:

<! DOCTYPE html> 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.