我正在尝试创建一个投票函数,在单击时增加类URL.votes +1。这是一个两部分问题:
如何拉实体键? (我认为你需要关键来区分哪个投票属性被修改?)
您如何为链接编写'a href'以进行投票?
谢谢!
型号:
类URL(db.Model):
user = db.ReferenceProperty(User)
website = db.StringProperty()
created = db.DateTimeProperty(auto_now=True)
votes = db.IntegerProperty(default=1)
类投票(db.Model):
user = db.ReferenceProperty(User) #See if voted on this site yet
url = db.ReferenceProperty(URL) #To apply vote to right URL
upvote = db.IntegerProperty(default=1)
created = db.DateTimeProperty(auto_now=True)
控制器
类VoteHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'base/index.html')
def post(self):
#See if logged in
self.Session = Session()
if not 'userkey' in self.Session:
doRender(
self,
'/',
{'error' : 'Please login to vote'})
return
#Get current vote total
url = db.URL.get() #pull current site. This is where I think I need the help
url.votes += 1 #pull current site vote total & add 1
url.put();
logging.info('Adding a vote'+nurl)
#Create a new Vote object
newvote = models.Vote(user=self.Session['userkey'], url=url)
newvote.put();
self.get();
self.redirect('/', { })
查看
a href =“/ vote”{{url.votes}}投票 - {{url.website}}
答案 0 :(得分:1)
答案包括以下几点:
a href =“/ vote?url_id = {{url.key.id}}”> {{url.votes}}投票 - {{url.website}} - {{ url.user.account}} - {{url.created | date:“M d,Y”}}
类VoteHandler(webapp.RequestHandler):
def get(self):
#See if logged in
self.Session = Session()
if not 'userkey' in self.Session:
doRender(
self,
'base/index.html',
{'error' : 'Please login to vote'})
return
#Get current vote total
key = self.request.get('url_id')
vurl = models.URL.get_by_id(int(key))
vurl.votes += 1 #pull current site vote total & add 1
vurl.put();
logging.info('Adding a vote')
#Create a new Vote object
newvote = models.Vote(user=self.Session['userkey'], url=vurl)
newvote.put();
self.redirect('/', { })
答案 1 :(得分:0)
您可以在视图中使用url.votes.key.id
a href =“/ vote *?id = {{url.votes.key.id}} *”{{url.votes}}投票 - {{url.website}}