1. 程式人生 > >學生資訊管理後臺(python+django+sql)

學生資訊管理後臺(python+django+sql)

一、功能:

  1. 實現對學生對個人資訊的增刪查改
  2. 實現後臺對所有學生資訊的操作

二、平臺:

  • windows+pycharm(python開發工具)

三、邏輯框圖

四、程式碼展示

  • 此後臺採用的是MVC的風格模式進行編碼

1.VIEWS部分

def hello(request):#功能選擇
    # t = loader.get_template("hello.html")
    # c = RequestContext(request)
    return render_to_response("hello.html",context_instance=RequestContext(request))

def
addstu(request):
# 新增學生資訊 name = request.POST.get("name") age = request.POST.get("age") learn = request.POST.get("learn") newstu = Student(name=name,age=age,learn=learn) newstu.save() # c = RequestContext(request) # t = loader.get_template("check.html") return render_to_response("done.html"
,context_instance=RequestContext(request,{"age":age,"learn":learn,"name":name})) def checkstu(request):#查詢學生資訊 find = [] stulist = Student.objects.all() name = request.POST.get("name") age = request.POST.get("age") learn = request.POST.get("learn") if name: find = Student.objects.filter(name__contains=name) elif
age: find = Student.objects.filter(age=age) elif age: find = Student.objects.filter(learn__contains=learn) return render_to_response("check.html",context_instance=RequestContext(request,{"stulist":stulist,"find":find})) def putmessage(request):#資訊提交中轉 return render_to_response("add.html",context_instance=RequestContext(request)) def deletStu(request):#刪除學生資訊 alert="" name = request.POST.get("name") age = request.POST.get("age") learn = request.POST.get("learn") delet = request.POST.get("delet") find = Student.objects.filter(name__contains =name) if not find: alert = u"沒有找到名為" + name + u"的學生" elif not delet: elert="" else: find.delete() alert = u"成功刪除該學生" return render_to_response("delet.html",context_instance=RequestContext(request,{"alert":alert,"student":find})) def putmessage2(request):#資訊提交中轉2 return render_to_response("put2.html",context_instance=RequestContext(request)) def rewrite(request):#學生資訊更改 aler=u"提示:" stulist = Student.objects.all() message = request.POST.get("message") old = request.POST.get("old") new = request.POST.get("new") if old == "name": student= Student.objects.filter( name__contains = message).update(name = new) aler+=u"資訊修改成功" elif old== "age": student= Student.objects.filter( name__contains = message).update(age = new) aler+=u"資訊修改成功" elif old =="learn": student= Student.objects.filter( name__contains = message).update(learn = new) aler+=u"資訊修改成功" else: aler+=u"請輸入正確的需要修改的資訊類別(name,age或learn)" return render_to_response("show.html",context_instance=RequestContext(request,{"id":id,"message":message,"new":new,"stulist":stulist,"aler":aler}))

2.MODELS 部分

class Student(models.Model):#學生模板
    name = models.CharField(max_length=20)
    age = models.IntegerField()
    learn = models.CharField(max_length=20)
    def __unicode__(self):
        return self.name

class Teacher(models.Model):#老師模板
    name = models.CharField(max_length=20)
    age = models.IntegerField()
    teach = models.CharField(max_length=20)
    student = models.ManyToManyField( Student,through = "OneClass")
    def __unicode__(self):
        return self.name

class OneClass(models.Model):#班級模板
    name = models.CharField(max_length=50)
    teacher = models.ForeignKey(Teacher)
    student = models.ForeignKey(Student)
    def __unicode__(self):
        return self.name

3.CONTROLLER部分

  • URL配置
urlpatterns = [url(r'^blog/','blog.views.hello'),
    url(r'^adstu/','blog.views.addstu'),
    url(r'^add/','blog.views.putmessage'),
    url(r'^check/','blog.views.checkstu'),
    url(r'^readydel/','blog.views.putmessage2'),
    url(r'^delet/','blog.views.deletStu'),
    url(r'^show/','blog.views.rewrite'),
    url(r'^admin/', include(admin.site.urls)),]
  • 表單的設定
__author__ = 'deven'
from django import forms
class ContentForm(forms.Form):
    username = forms.CharField()#max_length = 20)
    password = forms.IntegerField()#max_value= 20)

五、功能展示
1.主介面(功能選擇)
主介面

2.增加學生資訊
增添資訊

3.查詢學生資訊
查詢資訊前

4.刪除學生資訊
刪除資訊前
刪除確認

5.更改學生資訊
修改前
修改後