2014-01-06 27 views
2

我在同一主題上看到過類似的問題,它使用Python編寫了一個非常簡單的博客,並在GAE上託管。如果我錯過了其中一個答案中的解決方案,我很抱歉。Google App Engine上的簡單博客 - 沒有任何條目正在顯示

我看不到任何正在顯示的數據庫條目。這裏是我的代碼:

實體:

class Comment(db.Model): 
    name = db.StringProperty(required=True) 
    comment = db.TextProperty(required=True) 
    created = time.strftime("%d/%m/%Y") 

主要處理程序:

class MainPage(Handler): 
    def render_front(self, name="", comment="", error=""): 
     comments = db.GqlQuery("SELECT * FROM Comment ORDER BY created DESC") 
     self.render("front.html", name=name, comment=comment, error=error, comments=comments) 
    def get(self): 
     self.render_front() 
    def post(self): 
     name = self.request.get("name") 
     comment = self.request.get("comment") 
     if name and comment: 
      c = Comment(name=name, comment=comment) 
      c.put() 
      time.sleep(0.5) 
      self.redirect("/") 

因此,這將顯示在HTML:

{% for e in comments %} 
    <div class="comment"> 
     <div class="comment-name"> 
      {{e.name}} 
     </div> 
     <pre class="comment-content"> 
      {{e.comment}} 
      <br> 
      on {{e.created}} 
     </pre> 
    </div> 
{% endfor %} 

的問題是,該程序似乎完全忽略了上面的塊。我設法讓它工作了一段時間,但我多次檢查過,看不清問題出在哪裏。

任何幫助將不勝感激。提前致謝。

+0

你是如何設法使它工作的?您是否還檢查過App Engine日誌文件以查找錯誤?通常至少點你在正確的方向.. – Totem

+0

幾種方法來檢查通過這個: –

回答

3

一些方法通過這個檢查:

  • 使用管理控制檯來看看你的數據存儲。你有記錄嗎?

  • 您的datetime屬性是否正確存儲?有關DS和日期時間的文檔,請參見:https://developers.google.com/appengine/docs/python/datastore/typesandpropertyclasses#DateTimeProperty

  • 您是否將日期設置爲'dateTimeProperty'類型?請參閱此處的示例:developers.google.com/appengine/docs/python/ndb/queries另請參閱here:stackoverflow.com/questions/9700579/gql-select-by-date

  • 嘗試刪除ORDER BY子句在你的GQL中。我想知道是否有日期/時間變量有點時髦

  • 嘗試使用App Engine SDK本地運行,並使用其中的日誌查看行爲。當你滿意的時候,你可以把它上傳到GAE。

  • 不要使用模板引擎 - 現在只需在Python中完成循環 - 然後self.response.write的東西了。這會告訴你,如果您的查詢正常工作。

希望它適合您! :)

+1

感謝勞倫斯。所以在數據存儲中確實有記錄。刪除ORDER BY子句使它工作。無論什麼原因,當我將它添加到查詢中時,什麼都不顯示。如果我將創建的更改爲created = db.DateProperty(auto_now_add = True)並將GQL替換爲:comments = Comment.all()。order(' - created'),它仍然不起作用。 –

+0

您是否將日期設爲'dateTimeProperty'類型?請參閱此處的示例:https://developers.google.com/appengine/docs/python/ndb/queries 另請參閱此處:http://stackoverflow.com/questions/9700579/gql-select-by-date –

+0

將其更改爲'DateTimeProperty'確實可行。我現在可以按降序顯示所有評論。再次感謝(以及有用的鏈接)! –

相關問題