无法获得ndb查询结果

时间:2013-11-22 22:53:35

标签: html class python-2.7 app-engine-ndb time-and-attendance

我刚刚开始学习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)

1 个答案:

答案 0 :(得分:0)

尝试以下方法。我认为您的架构定义中存在一个小问题。

class Course(ndb.Model):
    code=ndb.StringProperty(indexed=True)
    title=ndb.StringProperty()