google app engine - How can I modify the property of ndb model through a webapp2 handler in python -
i have python file , html file interact each other through jinja2 environment in manner similar 1 in this tutorial.
the following code controls interaction between html file , python file:
class mainpage(webapp2.requesthandler): def get(self): submission_query = submission.query().order(-submission.score) submissions = submission_query.fetch(10) template_values = { 'submission' : submission, 'submissions' : submissions, } template = jinja_environment.get_template('index.html') self.response.write(template.render(template_values)) app = webapp2.wsgiapplication([ ('/', mainpage), ('/create', createsubmmission), ('/voteup', voteup), ], debug=true)
i have ndb model follows:
class submission(ndb.model): username = ndb.stringproperty() placename = ndb.stringproperty() link = ndb.stringproperty() score = ndb.integerproperty()
i have html form create new submission follows:
<form action="/create" method="post"> add new location: <br> name:<div><textarea name="username" rows="2" cols="60"></textarea></div> placename:<div><textarea name="placename" rows="2" cols="60"></textarea></div> url:<div><textarea name="link" rows="2" cols="60"></textarea></div> <div><input type="submit" value="post"></div> </form>
using request handler:
class createsubmmission(webapp2.requesthandler): def post(self): submission = submission() submission.username = self.request.get('username') submission.placename = self.request.get('placename') submission.link = self.request.get('link') submission.score = 0 submission.put() self.redirect('/')
i have section in html prints out each submission along button upvoting it:
{% submission in submissions %} <p> {{submission.username}} posted: <strong><a href="{{submission.link}}">{{submission.placename}}</a></strong> <br> score:{{submission.score}}<br> <!--vote button--> <form action="/voteup?submission={{submission}}" method="post"> <div><input type="submit" value="voteup"></div> </form> </p><br><br> {% endfor %}
the upvoting handled following python class:
class voteup(webapp2.requesthandler): def post(self): submission = self.request.get('submission') submission_key = submission.put() the_submission = submission_key.get() the_submission.score +=1 the_submission.put() self.redirect('/')
when pressing button, value of respective submission's score attribute should increase one.
the code implemented on website sunlit-hook-91816.appspot.com. can seen on site, pressing upvote button generates following error:
file "/base/data/home/apps/s~sunlit-hook-91816/1.383863233180213164/guestbook.py", line 52, in post submission_key = submission.put() attributeerror: 'str' object has no attribute 'put'
it appears voteup class somehow unable modify value of submission.score.
i using information found here can't figure out how correctly apply problem. can tell me of way make voteup class modify submission.score?
you're using submission object can passed along between html/python code can't, must add reference in form , dereference in server.
do in vote form:
<form action="/voteup?submission={{submission.key.urlsafe()}}" method="post"> <div><input type="submit" value="voteup"></div> </form>
we're getting key of submission in format safe http transport, can arrive server unaltered.
now when processing vote, can recreate key , correct object:
class voteup(webapp2.requesthandler): def post(self): submission = self.request.get('submission') submission_key = ndb.key(urlsafe=submission) the_submission = submission_key.get() the_submission.score +=1 the_submission.put() self.redirect('/')
as can see we're recreating key based on formatted string printed in form.
another way make better use input in form:
<form action="/voteup" method="post"> <input name="submission" type="hidden" value="{{submission.key.urlsafe()}}"> <div><input type="submit" value="voteup"></div> </form>
your code work same way, find easier read , maintain.
Comments
Post a Comment