简体   繁体   English

网址重写-Google App Engine(Python)

[英]URL Rewrite - Google App Engine (Python)

I followed a tutorial to create pretty URLs for my site that I plan to host on Google App Engine (Python). 我遵循了一个教程,为打算在Google App Engine(Python)上托管的网站创建漂亮的URL。

The problem is it won't display the index pages inside subdictories. 问题在于它不会在子索引内显示索引页。

I have a file called abc.html which loads fine at this address http:// 我有一个名为abc.html的文件,该文件可以在此地址http://上正常加载 www.testsite.com/abc www.testsite.com/abc

But I have index files in subdirectories (xyz and 123) which won't load into the content area (blank spacing in content area) 但是我在子目录(xyz和123)中有索引文件,这些文件不会加载到内容区域(内容区域中的空白)

Subdirectory xyz with index.html: http://www.testsite.com/xyz 带有index.html的子目录xyz: http : //www.testsite.com/xyz

Subdirectory 123 with index.html inside directory xyz: http://www.testsite.com/xyz/123 在目录xyz内具有index.html的子目录123: http : //www.testsite.com/xyz/123

Here is the code I used 这是我使用的代码

app.yaml app.yaml

application: testsite
version: 1
runtime: python
api_version: 1
threadsafe: yes

default_expiration: "1d"

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|swf|xml))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css|swf|xml))

- url: /.*
  script: main.py

main.py main.py

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

class MainPage(webapp.RequestHandler):
   def get(self, p):
      if p:
         page = p + ".html"
      else:
         p = "main"
         page = p + ".html"

          if not os.path.exists(page):
         page = "404.html"

      template_values = {
            "page" : page,
                p: "first", 
      };

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

application = webapp.WSGIApplication([(r'/(.*)', MainPage)],debug=True)

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
   main()

index.html index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   <title>TestTitle</title>
   <link href="/static/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
   <div id="header">
            <ul>
                <li><a href="main">Home</a></li>
                <li><a href="abc">abc</a></li>
                <li><a href="xyz/123">xyz</a></li>
            </ul>
   </div>

   <div id="content">
      <!-- this is where content will be loaded -->

      {% if page %}
         {% include page %}
      {% else %}
         {% include "main.html" %}
      {% endif %}

   </div>

   <div id="sidebar">
      TestSideBar
   </div>

   <div id="footer">
      TestFooter
   </div>
</body>
</html>

PS: The tutorial I followed was a dynamic page + URL rewrite guide. PS:我遵循的教程是动态页面+ URL重写指南。 The dynamic page aspect is not really necessary. 动态页面方面并不是真正必要的。 I just couldn't find a tutorial that would get pretty URLs working. 我只是找不到一个可以使漂亮的URL工作的教程。

First of all either your app.yaml or your main.py is erroneous. 首先,您的app.yaml或main.py错误。 Since Python27 is the more modern runtime, I suggest it's app.yaml I suggest the following app.yaml: 由于Python27是更现代的运行时,我建议使用app.yaml,建议使用以下app.yaml:

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

handlers:
- url: .*
  script: main.application

Then you talk about files called index.html inside subdirectories, but they are accessed nowhere. 然后,您将讨论子目录中名为index.html的文件,但是这些文件无处可访问。 Instead, you access 'xyz.html' and 'xyz/123.html'. 而是访问“ xyz.html”和“ xyz / 123.html”。

Try the following code snipped instead: 尝试替换以下代码:

        if p:
            page = os.path.join(p, "index.html")
        else:
            p = "main"
            page = "main.html"

By the way: You should consider template inheritance instead of the include tags! 顺便说一句:您应该考虑模板继承而不是include标记!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM