在Google App Engine中使用djangoTemplate和db.Model

时间:2011-12-21 19:23:03

标签: python django google-app-engine

我有一个小应用程序,其表格可以抓取有关新员工的数据,向人们发送电子邮件,并存储数据以备将来使用。一切都很好,但我似乎无法找到使用djangoforms和复选框的信息。这些显然是一个布尔值,但是如果我将db.Boolean值添加到我的db.Model类中,然后让它生成djangoforms.ModelForm类,则不会出现任何内容。我将它添加到我的db>模型中,它创建一个默认为false的记录,但是没有办法从表单修改。此外,db.Text也不会生成fiel。我可能在这里不够清楚,因为这是我的第一个GAE和Django项目。代码如下

import cgi
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import mail
from google.appengine.ext.db import djangoforms

class Item(db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  goes_by = db.StringProperty()
  Studio = db.StringProperty()
  Discipline = db.StringProperty()
  position = db.StringProperty()
  level = db.StringProperty(default='choice', choices=['choice', 'choice',
                                                          'choice', 'choice', 'choice', 'choice'])
  start_date = db.DateTimeProperty()
  buddy_name = db.StringProperty()
  past_employee = bool()
  email = db.EmailProperty()
  cell_phone = db.PhoneNumberProperty()
  phone = db.PhoneNumberProperty()
  hire_type = db.StringProperty(default='Full Time',choices=[
    'Temp', 'Intern', 'Contract', 'Full Time'])
  seating_location = db.StringProperty()
  additional_comments = db.Text()
  entry_time = db.DateTimeProperty(auto_now_add=True)
  added_by = db.UserProperty()

class ItemForm(djangoforms.ModelForm):
  class Meta:
    model = Item
    exclude = ['added_by']

class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('<html><body><head><link type="text/css" rel="stylesheet" href="/stylesheets/form.css"></head>'
                            '<div id=header><div id="logo><img src="/img/placeimg.gif"></div><div id ="headertxt"><h2>Place New Hire Portal</h2>'
                            '<p>Please filll out this form as accurately as possible, this information is used to create employee files and to setup IS equipment</div> '
                            '</div><div id="contain">'
                            '<form method="POST" '
                            'action="/">'
                            '<table>')
    # This generates our fields and writes it in the response
    self.response.out.write(ItemForm())
    self.response.out.write('</table>'
                            '<input type="submit">'
                            '</form></div></body></html>')


  def post(self):
    data = ItemForm(data=self.request.POST)
    if data.is_valid():
      # Save the data, and redirect to the view page
      entity = data.save(commit=False)
      entity.added_by = users.get_current_user()
      entity.put()
      self.redirect('/items.html')
      mailing_address = ("person@place.com")
      sender_address = ("person@place.com")
      subject = "A new hire has been made - Please begin Process"
      body = ("""
            The Following Person is scheduled to start at place on :

        %s %s - %s \ %s - %s - %s

        Please find the orientation template attached. In order to be sure all employees are oriented in the same way
        please be sure to cover the items listed under your name.

        In addition, afer the completion of your session, please escort the new hire to the next session and makle introductions. This will
        ensure that the schedule continues in order.

        Buddy, thank you very much for being a buddy to %s, Your time and lunch expenses should be charged to your Studio Operations number. Thank you
        very much for helping %s with their transiton to Place."""

                %(entity.first_name, entity.last_name, entity.Studio, entity.Discipline, entity.seating_location,
                entity.buddy_name, entity.first_name, entity.first_name))

      mail.send_mail(sender_address, mailing_address, subject, body)
    else:
      # Reprint the form
      self.response.out.write('<html><body>'
                              '<form method="POST" '
                              'action="/">'
                              '<table>')
      self.response.out.write(data)
      self.response.out.write('</table>'
                              '<input type="submit">'
                              '</form></body></html>')






class ItemPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write('</br></br><a href="/">Return to Form Entry</a></br></br>')
    query = db.GqlQuery("SELECT * FROM Item ORDER BY first_name")
    for item in query:
      self.response.out.write('<a href="/edit?id=%d">Edit</a> - ' %
                              item.key().id())
      self.response.out.write("%s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s </br>"%
                              (item.first_name, item.last_name, item.goes_by, item.Studio, item.Discipline, item.position,
                               item.level, item.start_date, item.past_employee, item.cell_phone, item.seating_location,
                               item.additional_comments, item.email, item.phone, item.hire_type))

class EditPage(webapp.RequestHandler):
  def get(self):
    id = int(self.request.get('id'))
    item = Item.get(db.Key.from_path('Item', id))
    self.response.out.write('<html><body>'
                            '<form method="POST" '
                            'action="/edit">'
                            '<table>')
    self.response.out.write(ItemForm(instance=item))
    self.response.out.write('</table>'
                            '<input type="hidden" name="_id" value="%s">'
                            '<input type="submit">'
                            '</form></body></html>' % id)

  def post(self):
    id = int(self.request.get('_id'))
    item = Item.get(db.Key.from_path('Item', id))
    data = ItemForm(data=self.request.POST, instance=item)
    if data.is_valid():
      # Save the data, and redirect to the view page
      entity = data.save(commit=False)
      entity.added_by = users.get_current_user()
      entity.put()
      self.redirect('/items.html')
    else:
      # Reprint the form
      self.response.out.write('<html><body>'
                              '<form method="POST" '
                              'action="/edit">'
                              '<table>')
      self.response.out.write(data)
      self.response.out.write('</table>'
                              '<input type="hidden" name="_id" value="%s">'
                              '<input type="submit">'
                              '</form></body></html>' % id)



def main():
  application = webapp.WSGIApplication(
                                       [('/', MainPage),
                                        ('/edit', EditPage),
                                        ('/items.html', ItemPage),
                                        ],
                                       debug=True)

  wsgiref.handlers.CGIHandler().run(application)

if __name__=="__main__":
  main()

1 个答案:

答案 0 :(得分:0)

Wobble为我回答了

应该是TextProperty和BooleanProperty。 db.Text只是datastore_types.Text的别名,而不是Property子类,db.Boolean应该引发异常,只要我能看到