Google App Engine永久链接页面

时间:2013-11-28 21:44:43

标签: python google-app-engine permalinks

我正在尝试构建一个简单的博客应用程序,以使用我从Udacity到目前为止所学到的技能。但是,我无法从数据库中检索数据并为用户显示数据。现在,我的博客有一个固定链接,显示用户刚刚提交的帖子,还有主要博客页面,它将按降序显示最新的10个帖子。但是当我提交帖子时,帖子成功存储在数据库中,我被重定向到永久链接。但是,我得到的只是一个404错误页面。

这是主要的Python代码:

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('<b>' + post.subject + '</b><br>')
    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', '<br>')
        return render_str("post.html", a = 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):
        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/<post_id:([0-9]+)>', DisplayPost)], debug=True)

以下是所有HTML页面基础的HTML代码:

<!DOCTYPE html>
<html>
<head>
  <link type="text/css" rel="stylesheet" href="/static/main.css" />

  <title>CS 253 Blog</title>
</head>

<body>
  <a href="/blog" class="main-title">
    CS 253 Blog
  </a>

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

</html>

以下是永久链接页面的HTML代码:

{% extends "base.html" %}

{% block content %}
  {{po.render() | safe}}
{% endblock %}

博客前面的HTML代码:

{% extends "base.html" %}

{% block content %}

  {% for p in posts %}
    {{ p.render() | safe }}
    <br><br>
  {% endfor %}

{% endblock %}

我的永久链接代码似乎有什么问题?

编辑1:

包括yaml文件:

application: deploymentapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /images
  static_dir: images

- url: /stylesheets
  static_dir: stylesheets

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest

0 个答案:

没有答案