Reputation: 11807
I am trying to make a basic quotation-sharing app using Webapp. Obviously it is crucial to be able to give arbitrary tags to each quote.
So here are the relevant code that I've come up with: (Mostly from the example chat app in the great introductory book 'Using Google App Engine' )
class Quote(db.Model):
user = db.ReferenceProperty()
text = db.StringProperty()
tags = db.StringListProperty()
created = db.DateTimeProperty(auto_now=True)
And the 'view':
class QuoteHandler(webapp.RequestHandler):
def get(self):
que = db.Query(Quote).order('-created');
chat_list = que.fetch(limit=10)
doRender(
self,
'quote.htm',
{ 'quote_list': quote_list })
def post(self):
self.session = Session()
if not 'userkey' in self.session:
doRender(self, 'quote.htm', {'error' : 'Must be logged in'} )
return
msg = self.request.get('message')
if msg == '':
doRender(self,'quote.htm',{'error' : 'Blank quote ignored'} )
return
tgs = self.request.get('tags') #really not sure of this
newq = Quote(user = self.session['userkey'], text=msg, tags= tgs)
newq.put();
self.get();
And in quote.htm I have:
{% extends "_base.htm" %}
{% block bodycontent %}
<h1>Quotes</h1>
<p>
<form method="post" action="/quote">
Quote:<input type="text" name="message" size="60"/><br />
Tags: <input type="text" name="tags" size="30"/>
<input type="submit" value="Send"/>
</form>
</p>
{% ifnotequal error None %}
<p>
{{ error }}
</p>
{% endifnotequal %}
<br />
<h3> The latest quotes </h3>
{% for quote in quote_list %}
<p>
{{ quote.text }}<br />
({{quote.user.account}}) <br />
{{ quote.tags }}
{{ quote.created|date:"D d M Y" }}
</p>
{% endfor %}
{% endblock %}
However, this combo is faulty. I get:
BadValueError: Property tags must be a list
No matter what I enter in the tags filed, and (obviously) I am new to both Python and Webapp. I googled a lot but could not find any guide to implement tags. So I really appreciate your help to fix this error, or rather point me to a a more elegant way to deal with tags.
Upvotes: 3
Views: 274
Reputation: 5955
Try using split()
to turn tgs
into a list of words before creating your Quote
. Tags should be separated by whitespace in your form, otherwise you can add an argument to split
if you'd rather separate them by something else.
...
tgs = self.request.get('tags').split()
newq = Quote(user = self.session['userkey'], text=msg, tags= tgs)
...
Upvotes: 4