我刚开始学习python ndb,我想知道如何显示参加所选课程的学生(筛选出勤率),然后用每个学生的ardio按钮标记他们的出勤率,将出勤值添加到上一个,最后将结果保存回数据存储或打印到文件
#--编码:cp1256--
import webapp2
import os
import cgi
from google.appengine.ext import ndb
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.ext.webapp import template
class Student(ndb.Model):
id = ndb.IntegerProperty()
name = ndb.StringProperty()
email = ndb.StringProperty()
#courses= ndb.StructuredProperty(Courses, repeated=True) and attendance
class Course(ndb.Model):
code=ndb.StringProperty()
title=ndb.StringProperty()
#time=ndb.TimeProperity
#students= ndb.StructuredProperty(Students, repeated=True)
#attendance
class Attendance(ndb.Model):
courseCode=ndb.StructuredProperty(Course)
#course=ndb.StructuredProperty(Course, repeated=True)
date=ndb.StringProperty()
#studentID=ndb.IntegerProperty(repeated=True)
student=ndb.StructuredProperty(Student, repeated=True)
attendance=ndb.IntegerProperty(repeated=True)# for each student
class MainHandler(webapp2.RequestHandler):
def get(self):
#cerate ndb from file
coursesfile = open('courses.txt', 'r').read()
studentsfile = open('students.txt', 'r').read()
dailyattendancefile = open('dailyattendance.txt','r').read()
for line in coursesfile.split('n'):
line=coursesfile.split('t')
#stroe courses to datastore
course=Course(code=line[0],title=line[1])#create Course entity
course.put()
for line in studentsfile.split('n'):
line=studentsfile.split('t')
student=Student(id=int(line[0]),name=line[1],email=line[2])
student.put()
for line in dailyattendancefile.split('n'):
line=dailyattendancefile.split('t')
attendance=Attendance(courseCode=Course(code=line[0]),date=line[1],student=Student(id=int(line[2])),attendance=int(line[3]))
attendance.put()
#print to html to test
#self.response.out.write("<tr><td>"+ course.code + "</td>")
#self.response.out.write("<td>"+ course.title+ "</td>")
#self.response.out.write("</tr>")
self.response.out.write("""
<html>
<body>
<form method="post" align="left">
<select align="center" name="course_code">
<option value="cs681" selected>CS681</option>
<option value="cs681">CS611</option>
</select>
<input type="submit" value="Submit"/>
""")
def post(self):
#get info from user
coursecode=self.request.get('course_code')
#self.response.out.write(Attendance.courseCode.code)
self.response.out.write("""
<table align="center" >
<tr align="center">
<td>Course code</td>
<td>Student ID</td>
<td>Date</td>
<td>Attendance</td>
</tr>
""")
qry=Attendance.query(Attendance.courseCode.code==coursecode).fetch()
for ent in qry:
self.response.out.write('<tr><td>%s</td></tr>' %ent.courseCode)
self.response.out.write("""
</table>
</form>
</body>
</html>
""")
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
尝试以下操作。我认为您的模式定义中有一个小问题。
class Course(ndb.Model):
code=ndb.StringProperty(indexed=True)
title=ndb.StringProperty()