【问题标题】:Using djangoTemplate and db.Model in Google App Engine在 Google App Engine 中使用 djangoTemplate 和 db.Model
【发布时间】:2011-12-21 19:23:02
【问题描述】:

我有一个小应用程序,它有一个表单,可以获取有关新员工的数据,通过电子邮件向人们发送相关信息,并存储数据以供将来使用。一切正常,但我似乎找不到有关使用 djangoforms 和复选框的信息。这些显然是一个布尔值,但是如果我将一个 db.Boolean 值添加到我的 db.Model 类中,然后将它放到 djangoforms.ModelForm 类中生成它,则什么也不会出现。我将它添加到我的 db>model 中,它创建了一个默认为 false 的记录,但无法从表单中进行修改。此外,db.Text 也不会产生字段。我可能在这里不够清楚,因为这是我的第一个 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()

【问题讨论】:

  • 应该是TextPropertyBooleanPropertydb.Text 只是 datastore_types.Text 的别名,而不是 Property 子类,据我所知,db.Boolean 应该引发异常。
  • 谢谢@Wooble db.Boolean,但 bool() 没有

标签: python django google-app-engine


【解决方案1】:

Wobble 替我回答了这个问题

应该是 TextProperty 和 BooleanProperty。 db.Text 只是 datastore_types.Text 的别名,而不是 Property 子类,据我所知,db.Boolean 应该引发异常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多