python - google appengine (python) 的基本 html 映射或 url 重写

标签 python google-app-engine url-rewriting url-routing friendly-url

我正在尝试为 Google Appengine 上的静态站点重写 URL。 我只要 http://www.abc.com/about对于 http://www.abc.com/about.html 我不需要重写像 abc.com/page?=1 之类的东西。 我只是想弄清楚如何显式重写 html 页面的 url。

我当前使用的代码(不起作用)-

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os

class MainHandler(webapp.RequestHandler):
    def get(self):
        template_values = {}

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))


class PostHandler(webapp.RequestHandler):
    def get(self, slug):
        template_values = {}
        post_list =  { 
            'home' : 'index.html',
            'portfolio' : 'portfolio.html',            
            'contact' : 'contact.html',
            'about' : 'about.html'
        }

        if slug in post_list:
            self.response.out.write('Slugs not handled by C-Dan yet')
        else:
            self.response.out.write('no post with this slug')

def main():
    application = webapp.WSGIApplication([('/', MainHandler),('/(.*)', PostHandler)], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

最佳答案

对于您的构造函数,您需要:

def main():
  application = webapp.WSGIApplication([
    ('/', MainHandler),
    ('/portfolio/', Portfolio),
    ('/contact/', Contant),
    ('/about/', About)
    ])
  util.run_wsgi_app(application)

这意味着任何时候有人去 http://www.abc.com/about/ ,它们将被“路由”到您的关于处理程序。

然后你必须制作一个关于处理程序。

class About(webapp.RequestHandler):
  def get(self):
    self.response.out.write(template.render('about.html', None))

我不熟悉您的编码风格,但我向您展示的内容在我的所有项目中都对我有用。

关于python - google appengine (python) 的基本 html 映射或 url 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290149/

相关文章:

google-app-engine - 使用谷歌登录 : How to customize the requested permissions

没有名字的 ASP.NET 网页,比如 stackoverflow?

python - 将 Dataframe 列 reshape 为行

python - 如何编写一个使用 <input type ="file"> 将数据上传到服务器的风车测试?

python - 从 peewee 模型中获取有序的字段名称

django - 我应该使用 google-app-engine-django 或 app-engine-patch 还是两者都不使用或其他?

python - 具有 unicode 字符的 SPARQL 正则表达式过滤器

java - 切换到 objectify 时,Google 云端点 .api 文件从 war 中删除

.htaccess - 使用 htaccess 将所有虚拟子域重写到同一文件夹

wordpress - 漂亮的 url 与 .htaccess?