所以我在python中有以下代码GAE代码并且它有点长但是基本上它允许用户发布到博客然后将每个博客帖子保存到用户被重定向到的永久链接并且用户可以重新链接永远。永久链接网址只是博客/(博客文章ID)。因此,通过重定向方法在NewPost处理程序中创建永久链接(如果我错了,请更正我),然后存储这些永久链接的列表在哪里?以某种方式可以访问吗?因为我看不到它在视觉上这部分让我吵了一下。
我从Udacity.com学到了所有这些代码,但他们并没有真正解释这一点,我可以让它工作,但不明白它是如何工作的。
import os
import re
from string import letters
import webapp2
import jinja2
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 BlogHandler(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)
#front page
class MainPage(BlogHandler):
def get(self):
self.write('Hello, Udacity!')
def blog_key(name = 'default'):
return db.Key.from_path('blogs', name)
#creates the database
class Post(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
last_modified = db.DateTimeProperty(auto_now = True)
def render(self):
self._render_text = self.content.replace('\n', '<br>')
return render_str("post.html", p = self)
#front of blog with list of last 10 posts
class BlogFront(BlogHandler):
def get(self):
posts = db.GqlQuery("select * from Post order by created desc limit 10")
self.render('front.html', posts = posts)
#displays the permalink page
class PostPage(BlogHandler):
def get(self, post_id):
key = db.Key.from_path('Post', int(post_id), parent=blog_key())
post = db.get(key)
if not post:
self.error(404)
return
self.render("permalink.html", post = post)
#create a new post
class NewPost(BlogHandler):
def get(self):
self.render("newpost.html")
def post(self):
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
p = Post(parent = blog_key(), subject = subject, content = content)
p.put()
self.redirect('/blog/%s' % str(p.key().id()))
else:
error = "subject and content, please!"
self.render("newpost.html", subject=subject, content=content, error=error)
app = webapp2.WSGIApplication([('/', MainPage),
('/unit2/welcome', Welcome),
('/blog/?', BlogFront),
('/blog/([0-9]+)', PostPage),
('/blog/newpost', NewPost),
],
debug=True)
更新:
是否可以通过以下方式更改代码,使其更具搜索引擎友好性:
class NewPost(BlogHandler):
def get(self):
self.render("newpost.html")
def post(self):
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
p = Post(parent = blog_key(), subject = subject, content = content)
p.put()
#the change is in the following line:
self.redirect('/blog/%s/%s' % (str(p.key().id(), str(p.subject)))
else:
error = "subject and content, please!"
self.render("newpost.html", subject=subject, content=content, error=error)
最终更新:
我解决了使用以下代码使搜索引擎更友好的问题:
def post(self):
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
p = Post(parent = blog_key(), subject = subject, content = content)
p.put()
subject = p.subject
subject = subject.replace(' ', '25fdsa67ggggsd5')
subject = ''.join(e for e in subject if e.isalnum())
subject = subject.replace('25fdsa67ggggsd5', '-')
subject = subject.lower()
self.redirect('/blog/%s/%s' % (str(p.key().id(), str(subject)))
else:
error = "subject and content, please!"
self.render("newpost.html", subject=subject, content=content, error=error)
答案 0 :(得分:3)
博客文章的ID是在数据存储区前检索它所需的全部内容,因此不需要任何类型的映射。一个请求带有永久链接URL,ID从URL中提取,使用ID从数据存储中检索帖子,呈现博客文章。
这是一个简单而有效的策略,但其中一个不平衡的领域是搜索引擎优化。大多数搜索引擎更喜欢其中包含单词的URL,而不仅仅是一串数字和字符。
这通常称为段塞,它需要您提供ID和字符串之间的映射。几年前,我编写了一个mixin类,为AppEngine / Python程序提供了简单的阻塞:sluggable-mixin。它应该仍然有效。