I’m new to Python, Google AppEngine and Jinja. If I’ve posted too much code below, I am sorry. This is my first posted question to StackOverflow.
I’m building a blog (for a class) and I need to replace newline characters “\n” in the user’s post. The function that queries the database sends it’s results to a render() function.
All the code works perfectly until I add the line of code that is supposed to replace the “\n.”
I’ve done a variety of edits and I just can’t deduce what the AppEngine, or Jinja, or Python wants me to do to satisfy it.
My database class sets up the db_blog Kind with four types, including ‘post_content‘
class db_blog(db.Model):
post_subject = db.StringProperty(required = True)
post_content = db.TextProperty(required = True)
post_created = db.DateTimeProperty(auto_now_add = True)
post_last_modified = db.DateTimeProperty(auto_now_add = True)
The ‘Newpost’ class I want this to receive the ‘query_select‘ object from the Blog class below.
class Newpost(webapp2.RequestHandler):
def write(self, *args, **kwds):
self.response.out.write(*args, **kwds)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kwds):
self._render_text = self.post_content.replace("\n", "<br />") ##<-- See here
self.write(self.render_str(template, **kwds))
The Blog class gets all user posts and sends them to ‘Newpost’ to be rendered.
class Blog(Newpost):
def get(self):
query_select = db.GqlQuery("SELECT * FROM db_blog ORDER BY post_created DESC")
self.render('blog_posts.htm', query_select = query_select)
1. As shown above, the code will generate this error:
AttributeError: ‘Blog’ object has no attribute ‘post_content’
I take it that class Blog needs to inherit from db_blog, so (See #2)
2. If I add db_blog to the Blog class’ parameter list I get a new error:
AttributeError: ‘NoneType’ object has no attribute ‘replace’
I think the error is telling me that I’m passing an object that doesn’t exist, so I’m really confused. If I comment-out the line trying to replace the “\n”character, this code runs perfectly – so how can it NOT be passing the object? Or claim it’s == None?
More importantly, how can I get it to work!
Much thanks to you who read this far, and kingdoms of wealth to s/he that can get me going.
The reason you are getting the attribute error is because the
selfinNewpost.getrefers to theRequestHandler(Blog) and not to the post that you want. I suggest that, instead of defining the conversion in your view function, added it as a property on the model, e.g.Then, you can just pass along
query_selectto your template (as you are currently doing now):You need to use safe above, because otherwise jinja will escape your
<br />. Since I can’t see you template, I don’t know if this exactly fits what you are doing, but hopefully you get the point.To explain the three lines of jinja above:
{% for post in query_select %}– just like a for loop, iterates over the posts, and each post should be adb_bloginstance (from your query).post.escaped_contentis exactly equivalent to calling theescaped_contentattribute of your db_blog model in Python.{{ post.escaped_content | safe }}applies thesafefilter to the string produced by escaped content (withoutsafe, jinja would convert<br />to<br />(which would literally display<br />on the page).{% endfor %}ends the for loop.