Question

I have a Blog model which contains 4 entities, subject, blog, time_created and day_created. I query all the values and assign it to an object which pass to a jinja2 template. In the template I iterate over the object and print each different entity which results in the display of each blog post.

The model for the data is:

class Blog(db.Model):
    subject = db.StringProperty(required = True, multiline = True)
    blog = db.TextProperty(required = True)
    time_created = db.DateTimeProperty(auto_now_add = True)
    day_created = db.DateProperty(auto_now_add = True)

And I query all the entries to display as individual posts as such:

posts = db.GqlQuery('SELECT * FROM Blog ORDER BY time_created DESC')

and I pass these out to the template

class Mainpage(BaseHandler):
def get(self):
    posts = Blog_posts()
    logging.info(posts)
    if posts:
        end_time = time.time() - start_time
        logging.info(end_time)
        logging.info(start_time)

    userLogin = self.checkLogin()
    cookieVal = self.read_secure_cookie('user-id')
    if cookieVal:
        u_id = cookieVal.split('|')[0]
        usr_instance = Users.get_by_id(int(u_id))
        if usr_instance:
            name = usr_instance.username
            if userLogin == True and name:
                self.render("blog.html", posts = posts, userLogin = userLogin, user_name = name, end_time = int(end_time))
                logging.info("Logged in = " + str(userLogin))               
        else:
            self.render("blog.html", posts = posts, userLogin = userLogin, user_name = "", end_time = int(end_time))        
    else:
        self.render("blog.html", posts = posts, userLogin = userLogin, user_name = "", end_time = int(end_time)) 

the function render() is :

def render(self, template, **kw):
    self.response.out.write(render_str(template, **kw))

I have a main template blogbase.html which I inherit to all other pages and it contains these lines which I use for template inheritance :

<div class="content">
  {% block content %}
  {% endblock %}
</div> 

and in the html file that displays the main blog page and inherits this is :

{% extends "blogbase.html" %}

{% block content %}

    {% for post in posts %}        
      <div class="one_post">          
        <div class="subject_title">
          <div class="day">{{post.day_created.strftime("%d %b %Y")}}</div>
          <a href="/blog/{{post.key().id()}}" target="_blank">   
            {{post.subject}}
          </a>
        </div>
        <div class="post">
            {{post.blog}}
        </div>
      </div>            
    {% endfor %}
    <div class="query">
      Queried {{end_time}} seconds ago
  </div>
{% endblock %}

I added a debugging line to see whether the query works and it returns the true with the following :

<google.appengine.ext.db.GqlQuery object at 0x7f99f03cd050>

But I am not able to iterate over this object as in the templates, regular html works, but the part between the for loop doesn't render.

Is there something wrong in my query ? Or am I using the templating engine wrong ?

Was it helpful?

Solution

2 things I've noticed.

You are using a query in your templates to iterate over it and do what? Print info? You need to get the entities. Either get them when you query or iterate and fetch.

posts = db.GqlQuery('SELECT * FROM Blog ORDER BY time_created DESC').fetch(1000)

Also if you are passing dicts to jinja you might wanny check that you are using {% for post in posts.iteritems() %} but this is not the case here.

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