2012-12-04 68 views
0

我想將python文件轉換爲python和html文件。我有的代碼基本上來自python留言板的例子,但我想有一個html文件服務於使用瀏覽器。在代碼中,我的作品,但現在當我在底部添加的JavaScript我得到一個錯誤此代碼是在ceemee11111.appspot.com如何將python文件分割爲python和html文件

import cgi 
import datetime 
import urllib 
import webapp2 

from google.appengine.ext import db 
from google.appengine.api import users 


class Greeting(db.Model): 
    """Models an individual Guestbook entry with an author, content, and date.""" 
    author = db.StringProperty() 
    content = db.StringProperty(multiline=True) 
    content2 = db.StringProperty(multiline=True) 
    date = db.DateTimeProperty(auto_now_add=True) 


def guestbook_key(guestbook_name=None): 
    """Constructs a Datastore key for a Guestbook entity with guestbook_name.""" 
    return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook') 


class MainPage(webapp2.RequestHandler): 
    def get(self): 
    self.response.out.write('<html><body>') 
    guestbook_name=self.request.get('guestbook_name') 


    greetings = db.GqlQuery("SELECT * " 
         "FROM Greeting " 
         "WHERE ANCESTOR IS :1 " 
         "ORDER BY date DESC LIMIT 3", 
         guestbook_key(guestbook_name)) 


    self.response.out.write(""" 
     <form action="/sign?%s" method="post"> 
     <div id="container" style="width:800px"> 

     <div id="header" style="background-color:#FFA500;"> 
     <h1 style="margin-bttom:0;">Main Title</h1></div> 

     <div id="Con0" style="background-color:#FFD700; 
    height:200px;width:200px;float:left;"> 
     <b>Menu</b><br> 
     HTML<br> 
     CSS<br> 
     <p id="demo1"></p><br> 
     JavaScript</div> 

     <div id="content" style="background-color:#EEEEEE;height:200px;width:600px;float:left;"> 
Content goes here</div> 
     </div> 
     <button onclick="myFunction()">Try it</button> 
     </form> 
    </body> 
    </html>""") 


self.response.out.write(""" 
     <form action="/sign?%s" method="post"> 
     <div id="dataImput" 
     <div><textarea name="content" rows="1" cols="10"></textarea></div> 
     <div><textarea name="content2" rows="1" cols="10"></textarea></div> 
     <div><input type="submit" value="Sign Guestbook"></div> 
     </div> 
     </form> 
     <hr> 
     <form>Guestbook name: <input value="%s" name="guestbook_name"> 
     <input type="submit" value="switch"></form> 
    </body> 
    </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}), 
         cgi.escape(guestbook_name))) 


class Guestbook(webapp2.RequestHandler): 
    def post(self): 
    # We set the same parent key on the 'Greeting' to ensure each greeting is in 
    # the same entity group. Queries across the single entity group will be 
    # consistent. However, the write rate to a single entity group should 
    # be limited to ~1/second. 
    guestbook_name = self.request.get('guestbook_name') 
    greeting = Greeting(parent=guestbook_key(guestbook_name)) 

    if users.get_current_user(): 
     greeting.author = users.get_current_user().nickname() 

greeting.content = self.request.get('content') 
greeting.content2 = self.request.get('content2') 
greeting.put() 
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name})) 


app = webapp2.WSGIApplication([('/', MainPage), 
          ('/sign', Guestbook)], 
          debug=True) 

#<script> 
#function myFunction() 
#{ 
#document.getElementById("demo1").innerHTML="test"; 
#} 

謝謝你們的時間。 Dan

回答

1

鑑於您明確使用GAE,您所要做的是概述here

首先,將您的html移動到單獨的文件。我們會從奇怪的自由飄浮的self.response.out.write的HTML開始:

""" 
     <form action="/sign?%s" method="post"> 
     <div id="dataImput" 
     <div><textarea name="content" rows="1" cols="10"></textarea></div> 
     <div><textarea name="content2" rows="1" cols="10"></textarea></div> 
     <div><input type="submit" value="Sign Guestbook"></div> 
     </div> 
     </form> 
     <hr> 
     <form>Guestbook name: <input value="%s" name="guestbook_name"> 
     <input type="submit" value="switch"></form> 
    </body> 
    </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}), 
         cgi.escape(guestbook_name)) 

這屬於一個單獨的文件,我們會打電話給myhtml.html。其次,我們接下來將使用Django模板系統,而​​不是%s和Python字符串格式。因此,首先,我們要更換%s{{ }}包圍模板友好的領域,給我們:

""" 
     <form action="/sign?{{ guestbook_name }}" method="post"> 
     <div id="dataImput" 
     <div><textarea name="content" rows="1" cols="10"></textarea></div> 
     <div><textarea name="content2" rows="1" cols="10"></textarea></div> 
     <div><input type="submit" value="Sign Guestbook"></div> 
     </div> 
     </form> 
     <hr> 
     <form>Guestbook name: <input value="{{ guestbook_name|escape }}" name="guestbook_name"> 
     <input type="submit" value="switch"></form> 
    </body> 
    </html>""" 

需要注意的是,我們能夠使用escapetemplate filter,這是|後會發生什麼,到逃避我們從guestbook_name獲得的價值。

最後,我們加載HTML,並通過它,我們需要的參數:

self.response.out.write(template.render('myhtml.html', {'guestbook_name': guestbook_name})) 
+0

我複製了你的代碼,我想我已經接近了,但是回溯的最後一行是....文件「C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ _internal \ django \ template \ loader。 py「,第138行,在find中_template raise TemplateDoesNotExist(name) TemplateDoesNotExist:myhtml.html ...所以我認爲有一個路徑問題 –

1

我認爲你的代碼將真正受益於從Python代碼中分離HTML。

你可以通過編寫一個html模板來做到這一點,並使用jinja2(附帶谷歌appengine)來生成html。有關於如何做到這一點的說明here。 JavaScript將進入HTML模板,而不是Python代碼。

+0

你救了我很多時間和這個答案解決了這個問題,但我得到的和錯誤文件「C:\ Program Files文件(86 )\ Google \ google_appengine \ google \ appengine \ _internal \ jinja \ template \ loader.py「,第138行,在find_template中 raise TemplateDoesNotExist(name) TemplateDoesNotExist:index.html ...所以我認爲這是一個路徑問題 –