簡體   English   中英

網址重寫-Google App Engine(Python)

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

我遵循了一個教程,為打算在Google App Engine(Python)上托管的網站創建漂亮的URL。

問題在於它不會在子索引內顯示索引頁。

我有一個名為abc.html的文件,該文件可以在此地址http://上正常加載 www.testsite.com/abc

但是我在子目錄(xyz和123)中有索引文件,這些文件不會加載到內容區域(內容區域中的空白)

帶有index.html的子目錄xyz: http : //www.testsite.com/xyz

在目錄xyz內具有index.html的子目錄123: http : //www.testsite.com/xyz/123

這是我使用的代碼

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

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

<!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:我遵循的教程是動態頁面+ URL重寫指南。 動態頁面方面並不是真正必要的。 我只是找不到一個可以使漂亮的URL工作的教程。

首先,您的app.yaml或main.py錯誤。 由於Python27是更現代的運行時,我建議使用app.yaml,建議使用以下app.yaml:

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

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

然后,您將討論子目錄中名為index.html的文件,但是這些文件無處可訪問。 而是訪問“ xyz.html”和“ xyz / 123.html”。

嘗試替換以下代碼:

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

順便說一句:您應該考慮模板繼承而不是include標記!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM