Have you read the docs for ndb queries. https://developers.google.com/appengine/docs/python/ndb/queries
It has a good example of constructing a query in multiple parts
qry1 = Account.query() # Retrieve all Account entitites
qry2 = qry1.filter(Account.userid >= 40) # Filter on userid >= 40
qry3 = qry2.filter(Account.userid < 50) # Filter on userid < 50 as well
In your example you would
messages = Message.query()
if title:
messages = messages.filter(Message.title==title)
if account:
messages = messages.filter(Message.account==account)
Calling query() in the code you have in the question creates a new query, rather than extending it.
Read further in the docs, and you can see you can also start using OR operators to construct the query in multiple phases. The example above means AND.