wns9778.com_威尼斯wns.9778官网

热门关键词: wns9778.com,威尼斯wns.9778官网
wns9778.com > 计算机教程 > python django sql学生信息管理后台开发

原标题:python django sql学生信息管理后台开发

浏览次数:197 时间:2019-06-15

一、功能

本文研究的主要是Python web开发框架Django的管理界面的相关内容,具体如下。

实现对学生对个人信息的增删查改
实现后台对所有学生信息的操作

admin界面的路径,通常在my_project中已经设置好了,使用django-admin.py startproject my_project

二、平台

在目录下的urls.py文件中可以看到

windows pycharm(python开发工具)

from django.conf.urls import include, url 
from django.contrib import admin 

urlpatterns = [ 
  # Examples: 
  # url(r'^$', 'my_project.views.home', name='home'), 
  # url(r'^blog/', include('blog.urls')), 

  url(r'^admin/', include(admin.site.urls)), 
  url(r'^$', 'my_project.views.first_page'), 
  url(r'^app1/', include('app1.urls')), 
] 

三、逻辑框图

将数据模型Student交给admin管理界面管理,需要在admin中注册该model

四、代码展示

在该app的admin.py中加上:

此后台采用的是MVC的风格模式进行编码

from django.contrib import admin 
from app1.models import Student 


# Register your models here. 
admin.site.register(Student) 

1.VIEWS部分

访问:http://localhost:8000/admin/

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}))

登陆之后就可以管理我们的model了

wns9778.com,2.MODELS 部分

wns9778.com 1

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部分

界面的汉化很简单,只需要将setting.py中的 LANGUAGE_CODE = 'en-us' 修改为 zh-cn

URL配置

总结

以上就是本文关于Python学习之Django的管理界面代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

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)),]

您可能感兴趣的文章:

表单的设置

本文由wns9778.com发布于计算机教程,转载请注明出处:python django sql学生信息管理后台开发

关键词: wns9778.com

上一篇:简单了解python模块概念【wns9778.com】

下一篇:没有了