无法根据 Python GAE 中的密钥在浏览器中查看项目

发布于 2024-12-07 05:14:20 字数 1268 浏览 1 评论 0原文

我正在使用 python GAE 和 webapp。

我有一个供用户在数据库中创建对象的表单,类似于:

class SpamRecord(db.Model):
    author = db.ReferenceProperty(Author, required=True)
    text = db.StringProperty()

创建后,用户将被重定向到 URL 包含该对象的键的页面...使用如下代码:

spam = SpamRecord(author=author, text=text)
spam.put()
new_spam_key = spam.key()
self.redirect("/view_spam/%s" % new_spam_key)

而这个主要< /em> 有效,我可以在以下位置查看项目:

sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQYy8oJDA sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY_boJDA

但是,偶尔会有一个键不起作用。以下是最近 2 个无法加载并返回 HTTP 404 not found 错误的页面示例:

sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY-5MJDA sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY-boJDA

我的 html-mappings.py 包含以下映射:

(r"/view_spam/(\w+)", ViewSpamPage)

并且 ViewSpamPage 看起来像这样:

class ViewSpamPage(webapp.RequestHandler):
    def get(self, spam_id):
        self.response.out.write("Got here")

任何人都可以提供任何关于为什么会这样的见解吗?正在发生以及如何预防?

非常感谢!

I'm using python GAE with webapp.

I have a form for a user to create a object in the database, something like:

class SpamRecord(db.Model):
    author = db.ReferenceProperty(Author, required=True)
    text = db.StringProperty()

After it's created, the user is redirected to a page whose URL contains that object's key... using code such as:

spam = SpamRecord(author=author, text=text)
spam.put()
new_spam_key = spam.key()
self.redirect("/view_spam/%s" % new_spam_key)

And this mostly works, with me being able to view items at:

sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQYy8oJDA
sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY_boJDA

However, there's an occasional key that won't work. Here are 2 recent examples of pages that won't load and return HTTP 404 not found errors:

sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY-5MJDA
sitename.com/view_spam/ag1waWNreXByZXNlbnRzchQLEgxBbm5vdW5jZW1lbnQY-boJDA

My html-mappings.py contains the following mapping:

(r"/view_spam/(\w+)", ViewSpamPage)

And the ViewSpamPage looks something like:

class ViewSpamPage(webapp.RequestHandler):
    def get(self, spam_id):
        self.response.out.write("Got here")

Can anyone offer any insight as to why this is occurring and how it may be prevented?

Thanks very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-12-14 05:14:20

在正则表达式中,\w 与连字符不匹配。 (它将匹配下划线。)对于第二对键,这将导致仅将键的部分传递给您的处理程序。

在您的网址模式中,请尝试使用 r"/view_spam/(.*)"

In regular expressions, \w doesn't match hyphens. (It will match underscores.) For that second pair of keys, this'll result in only passing part of the key to your handler.

In your URL pattern, try r"/view_spam/(.*)" instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文