我建模了一对多关系,其中一个用户可以卖出很多东西。
在卖家页面上,我有用户输入的用户信息:first_name,last_name,email
和他们想要销售的商品信息:item_name,item_price,item_tag。
我没有使用参考资产吗?当localhost运行时,页面为空。
class UserModel(db.Model):
first_name = db.StringProperty(required = True)
last_name = db.StringProperty(required = True)
email = db.StringProperty(required = True)
class SellModel(db.Model):
user = ReferenceProperty(UserModel, collection_name = "sells")
item_name = db.StringProperty(required = True)
item_price = db.StringProperty(required = True)
item_tag = db.StringProperty(required = True)
class Sell(Handler):
def get(self):
self.render("sell.html")
def post(self):
item_name = self.request.get('item_name')
item_price = self.request.get('item_price')
item_tag = self.request.get('item_tag')
first_name = self.request.get('first_name')
last_name = self.request.get('last_name')
email = self.request.get('email')
if item_name and item_price and item_tag and first_name and last_name and email:
user = UserModel(key_name = "user",
first_name = first_name, last_name = last_name,
email = email)
user.put()
sell = SellModel(user = user, item_name = item_name, item_price = item_price, item_tag = item_tag)
sell.put()
stat = "your item has been recorded"
self.render("sell.html", stat = stat)
else:
error = "make sure you fill out every box"
self.render("sell.html",
item_name = item_name, item_price = item_price, item_tag = item_tag,
first_name = first_name, last_name = last_name,
email = email, error=error)
sell.html:
<form method="post">
<label>item name</label>
<input type="text" name="item_name" value="{{item_name}}">
<label>item price</label>
<input type="text" name="item_price" value="{{item_price}}">
<label>tag</label>
<input type="text" name="item_tag" value="{{item_tag}}">
<label>first name</label>
<input type="text" name="first_name" value="{{first_name}}">
<label>last name</label>
<input type="text" name="last_name" value="{{last_name}}">
<label>email</label>
<input type="text" name="email" value="{{email}}">
<div><input type="submit" value="Okay"></div>
<div class = "stat">{{stat}}</div>
<div class="error">{{error}}</div>
</form>
当我删除
user = ReferenceProperty(User, collection_name = "sells")
并且只提交用户并出售,页面显示。什么是这条线弄乱了页面?
编辑: 缩进来自复制和粘贴,并且在代码中。在我继承的Handler类中有
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
所以问题不应该在get方法中。
答案 0 :(得分:1)
你的get方法应该是这样的:
def get(self):
f=open('sell.html','r')
sourceHtml = unicode(f.read(),errors='ignore')
f.close()
self.response.write(sourceHtml)
我也看到了缩进问题。类Sell与get()方法的级别相同。该方法应该在内部一级。
答案 1 :(得分:1)
刚刚发现了这个bug。忘了把“db”。在ReferenceProperty之前。 LOL