Question

I am running python in GAE and am stuck on a few questions related to GQL queries and using templates.

When I load the homepage, it triggers this function:

class MainPage()
    def get(self):
            product_list = Post.gql("ORDER BY product LIMIT 10")
            self.render('index.html', product_list = product_list)

    def post(self):

            email = self.request.get('email')
            product = self.request.get('product')

            if email and product:
                p = Post(parent = blog_key(), email = email, product = product)
                p.put()

It should pass a list of the products to the homepage, as well as accept new entries through a form on the homepage.

index.html:

<form id="inputs" method="post">
   <input id="product" type="text" name="product" value="product?" value="{{product}}"></input>
   <input type="text" name="email" id="email" value="email?" value="{{email}}"></input>
   <button type="submit" value="submit!">Submit</button>
</form>
<div id="results">
   {% for p in product_list %}
       {{p.product}}<br>
       {{p.email}}
        <br><br>
   {% endfor %}
</div>

When I load the homepage, nothing shows up in the "results" div. In the GAE admin console, I can see that I have a few "Post" entities that should show up.

Was it helpful?

Solution

Take a look at the docs here for querying:

Your query part should look something like this:

product_list = db.GqlQuery("SELECT * FROM Post")

OR for NDB:

product_list = ndb.gql("SELECT * FROM Post")

OTHER TIPS

Try explicitly executing the query with run():

class MainPage()
    def get(self):
            product_list = Post.gql("ORDER BY product LIMIT 10").run()
            self.render('index.html', product_list = product_list)

The gql object should be implictly run when it is used as an iterable, but I do not know if that will work with jinja.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top