Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle App Engine: Retrieving data from database
    text
    copied!<p>I am trying to build a simple blog application to use the skills that I learned from Udacity so far. However, I am having trouble retrieving data from the database and displaying it for the user. Now, my blog has a permalink which displays the post that was just being submitted by the user, and also the main blog page which will display the latest 10 posts in descending order. But when I submit a post, the post is stored in the database successfully, and I am being redirected to a permalink. However, all I get is a blank page instead of the post that I just submitted. </p> <p>Also, when I go back to my main blog page, I see this instead of all the posts being submitted by the user:</p> <p><img src="https://i.stack.imgur.com/72b9F.png" alt="enter image description here"></p> <p>Here's the main python code:</p> <pre><code>import os import re import webapp2 import jinja2 from string import letters from google.appengine.ext import db template_dir = os.path.join(os.path.dirname(__file__), 'templates') jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True) def render_str(template, **params): t = jinja_env.get_template(template) return t.render(params) class Handler(webapp2.RequestHandler): def write(self, *a, **kw): self.response.out.write(*a, **kw) def render_str(self, template, **params): return render_str(template, **params) def render(self, template, **kw): self.write(self.render_str(template, **kw)) def render_post(response, post): response.out.write('&lt;b&gt;' + post.subject + '&lt;/b&gt;&lt;br&gt;') response.out.write(post.content) def post_key(name = "dad"): return db.Key.from_path('blog', name) class Blogger(db.Model): name = db.StringProperty() content = db.TextProperty() created = db.DateTimeProperty(auto_now_add = True) def render(self): self._render_text = self.content.replace('\n', '&lt;br&gt;') return render_str("post.html", p = self) class MainPage(Handler): def get(self): self.response.write("Visit our blog") class BlogHandler(Handler): def get(self): posts = db.GqlQuery("SELECT * FROM Blogger order by created desc") self.render("frontblog.html", posts = posts) class SubmitHandler(Handler): def get(self): self.render("temp.html") def post(self): name = self.request.get("name") content = self.request.get("content") if name and content: a = Blogger(parent = post_key(), name = name, content = content) a.put() self.redirect('/blog/%s' % str(a.key().id())) else: error = "Fill in both the columns!" self.render("temp.html", name = name, content = content, error = error) class DisplayPost(Handler): def get(self, post_id): post_id = self.request.get("post_id") if post_id: po = Blogger.get_by_id(int(post_id), parent = post_key()) if po: self.render("perma.html", po = po) self.response.write("No") app = webapp2.WSGIApplication([('/', MainPage), ('/blog', BlogHandler), ('/blog/submit', SubmitHandler), (r'/blog/&lt;post_id:([0-9]+)&gt;', DisplayPost)], debug=True) </code></pre> <p>Here's the HTML code for the base of all the HTML pages:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;link type="text/css" rel="stylesheet" href="/static/main.css" /&gt; &lt;title&gt;CS 253 Blog&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="/blog" class="main-title"&gt; CS 253 Blog &lt;/a&gt; &lt;div id="content"&gt; {% block content %} {% endblock %} &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's the HTML code for the permalink page:</p> <pre><code>{% extends "base.html" %} {% block content %} {{po.render() | safe}} {% endblock %} </code></pre> <p>HTML code for the front of the blog:</p> <pre><code>{% extends "base.html" %} {% block content %} {% for p in posts %} {{ p.render() | safe }} &lt;br&gt;&lt;br&gt; {% endfor %} {% endblock %} </code></pre> <p>I have been struggling with this for over two days now. I see no bugs in the logs either. What seems to be the problem?</p> <hr> <p>EDIT:</p> <p>Edited the source code based on the answers below. However, I still get a 404 error.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload