当用户创建新事件时,他们输入“event_name”和“event_time”并从下拉列表中选择位置。然后,我从与该位置匹配的PlaceModel实例中检索事件的纬度和经度。
问题是,当我正在寻找的PlaceModel实例存在时,localhost返回Nonetype。
我的模特:
class PlaceModel(db.Model):
place_name = db.StringProperty(required = True)
place_lat = db.StringProperty(required = True)
place_long = db.StringProperty(required = True)
def render(self):
return render_str("placemodel.html", p = self)
class EventModel(db.Model):
event_place = db.ReferenceProperty(PlaceModel)
event_name = db.StringProperty(required = True)
event_time = db.StringProperty(required = True)
def render(self):
return render_str("eventmodel.html", e = self)
html:
<select name = "place_name">
{% for place in allplaces %}
<option name = "place_name" value = {{place_name}}> {{place.place_name}}</option>
{%endfor%}
</select>
蟒蛇:
class NewEvent(Handler):
def get(self):
if self.user:
allplaces = PlaceModel.all()
self.render("newevent.html", allplaces = allplaces)
else:
self.redirect("/signup")
def post(self):
event_name = self.request.get("event_name")
event_time = self.request.get("event_time")
place_name = self.request.get_all("place_name")
if event_name and event_time and place_name:
place_lat = PlaceModel.gql("where place_name = :place_name", place_name = place_name).get().place_lat
place_long = PlaceModel.gql("where place_name = :place_name", place_name = place_name).get().place_long
event_place = PlaceModel(parent = place_key(),
place_name = place_name, place_lat = place_lat, place_long = place_long)
event = EventModel(parent = event_key(),
event_name = event_name, event_time = event_time,
event_place = event_place)
event.put()
stat = "your event has been recorded"
self.render('newevent.html', stat = stat)
else:
allplaces = PlaceModel.all()
error = "fill in all the boxes"
self.render("newevent.html", error = error, allplaces = allplaces,
event_name = event_name, event_time = event_time)
怎么了?为什么gql返回nonetype?
编辑: 错误日志的(后半部分):
File "/Users/yingjiefu/Documents/munchkin/munchkin.py", line 195, in post
place_lat = PlaceModel.gql("where place_name = :place_name", place_name = place_name).get().place_lat
AttributeError: 'NoneType' object has no attribute 'place_lat'
编辑:它现在有效!
python:
class NewEvent(Handler):
def get(self):
if self.user:
allplaces = PlaceModel.all()
self.render("newevent.html", allplaces = allplaces)
else:
self.redirect("/signup")
def post(self):
event_name = self.request.get("event_name")
event_time = self.request.get("event_time")
place_name = self.request.get("place_name")
if event_name and event_time and place_name:
place = PlaceModel.all().filter("place_name", place_name).get()
place_lat = place.place_lat
place_long = place.place_long
event = EventModel(parent = event_key(),
event_name = event_name, event_time = event_time,
event_place = place)
event.put()
stat = "your event has been recorded"
allplaces = PlaceModel.all()
self.render('newevent.html', stat = stat, allplaces = allplaces)
else:
allplaces = PlaceModel.all()
error = "fill in all the boxes"
self.render("newevent.html", error = error, allplaces = allplaces,
event_name = event_name, event_time = event_time)
html:
<select name = "place_name" >
{% for place in allplaces %}
<option value = "{{ place.place_name }}">{{ place.place_name }}</option>
{% endfor %}
</select>
答案 0 :(得分:0)
此代码存在很多问题。
我希望主要的是如何定义选项中的值。您不仅无法将其包装在引号中,而且实际上使用了一个不存在的变量:place_name
。它应该是这样的:
<option name="place_name" value="{{place_name}}">{{place.place_name}}</option>
其次,从请求中提取get_all()
值时使用place_name
。据我了解,它返回列表,因此您在GQL中的占位符中将有多个值。您应该像对待其他变量一样使用get()
。
第三,与您的问题无关,但为什么要两次执行完全相同的查询?无缘无故地花费你两倍的费用。获取一次并将其存储在变量中:
place = PlaceModel.gql("where place_name = :place_name", place_name=place_name).get()
place_lat = place.place_lat
place_long = place.place_long
(我甚至不会问你为什么要使用GQL而不是普通的Query语法,这会短得多:PlaceModel.all().filter('place_name', place_name)
- 你确实意识到它们都被转换为相同的RPC调用,并且GQL在任何方面都不比模型版本更“原生”?)