Google App Engine:404找不到资源
我想在Python中使用Google App Engine建立一个基本的网志模型。但是,我的代码的问题,我想,我得到一个404错误,当我试图显示所有发布的博客条目在一个页面上。这里是python代码:
I am trying to build a basic blog model using Google App Engine in Python. However, something's wrong with my code I suppose, and I am getting a 404 error when I try to display all the posted blog entries on a single page. Here's the python 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)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
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", 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(name = name, content = content, parent = post_key())
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))
if po:
self.render("perma.html", po = po)
else:
self.response.write("404 Error")
app = webapp2.WSGIApplication([('/', MainPage),
('/blog', BlogHandler),
('/blog/submit', SubmitHandler),
('/blog/([0-9]+)', DisplayPost)], debug=True)
发布我的内容后,它被重定向到一个固定链接。但是,这是我提交我的帖子的错误:
After posting my content, it gets redirected to a permalink. However, this is the error I am getting on submitting my post:
404 Not Found
The resource could not be found
这里是frontblog.html源代码,万一有帮助:
Here's the frontblog.html source code, in case that would help:
<!DOCTYPE html>
<html>
<head>
<title>CS 253 Blog</title>
</head>
<body>
<a href="/blog">
CS 253 Blog
</a>
<div id="content">
{% block content %}
{%for post in posts%}
{{post.render() | safe}}
<br></br>
{%endfor%}
{% endblock %}
</div>
</body>
</html>
所以基本上,我没有被重定向到永久链接页面。
So basically, I am not being redirected to the permalink page. What seems to be the problem?
当你创建你的帖子时,你会给它一个父(不知道为什么) 。但是当你得到它,你这样做的ID只,不考虑父ID。在数据存储区中,键实际上是一个由所有父种类和ID /名称组成的路径,然后是当前实体的路径,并获取您需要传递完整路径的对象。
When you create your post, you're giving it a parent (not sure why). But when you get it, you do so by the ID only, and don't take into account the parent ID. In the datastore, a key is actually a path consisting of all the parent kinds and IDs/names and then those of the current entity, and to get an object you need to pass the full path.
可能的解决方案:
- 删除父键,因为它不会在这里做任何事将其设置为相同的值;
- 在获取对象时使用它:
Blogger.get_by_id(post_id,parent = parent_key())
- - 显然这只有在父项始终相同时才有效; - 在路径中使用完整的字符串键,而不仅仅是ID,并执行
Blogger。 get(key)
- 您还需要更改路由正则表达式以接受字母数字字符,例如'/ blog /(\w +)'
,并将重定向更改为'/ blog /%s'%a.key()
。
- Drop the parent key, since it isn't doing anything here as you're always setting it to the same value;
- Use it when you get the object:
Blogger.get_by_id(post_id, parent=parent_key())
- obviously this only works if the parent is always the same; - Use the full stringified key in the path, rather than just the ID, and do
Blogger.get(key)
- you'll also need to change the route regex to accept alphanumeric chars, eg'/blog/(\w+)'
, and change the redirect to'/blog/%s' % a.key()
.