2017-03-04 49 views
0

我想創建一個使用Web2Py/Python的評論部分,我創建了沒有錯誤的表單,但是當表單提交時註釋並沒有完全添加。任何人都可以發現我錯過的東西嗎?輸入不是從Web2Py/Python表單提交

db1.py模式:

db.define_table('products', 
      Field('Product_Name',requires=IS_NOT_EMPTY()), 
      Field('Product_Description',requires=IS_NOT_EMPTY()), 
      Field('Product_Review',requires=IS_NOT_EMPTY()), 
      auth.signature) 

db.define_table('product_comments', 
      Field('products', 'reference products'), 
      Field('body', 'text', requires=IS_NOT_EMPTY()), 
      auth.signature) 

default.py控制器:

def show(): 
post = db.products(request.args(0, cast=int)) 
productDescription = T("Product Description") 
productReview = T("Product Review") 
back = T("Back") 
#commentHeading = T("Comments") 
db.product_comments.products.default = post.id 
db.product_comments.products.readable = False 
db.product_comments.products.writable = False 
comments = db(db.product_comments.products==post.id).select() 
form = SQLFORM(db.product_comments).process() 
return locals() 

默認/ show.html觀點:

{{extend 'layout.html'}} 

<h1>{{=XML(post.Product_Name, sanitize=True)}}</h1> 

<h2>{{=XML(productDescription, sanitize=True)}}</h2> 
{{=XML(post.Product_Description, sanitize=True)}} 


<h2>{{=XML(productReview, sanitize=True)}}</h2> 
{{=XML(post.Product_Review, sanitize=True)}} 

<h2>Comments</h2> 

{{for comment in comments:}} 
<div class="well"> 
    {{=comment.created_by.first_name}} {{=comment.created_by.last_name}} 
    on {{=comment.created_on}} says 
    {{comment.body}} 
</div> 
{{pass}} 

{{=XML(form, sanitize=True)}} 

<a href="/ReviewMyProduct/default/index">{{=XML(back, sanitize=True)}}</a> 

回答

0

我需要一個=內部{{comment.body}}使其看起來像{{=comment.body}}。然而,如果你想讓評論部分根據索引顯示主體,Anthony的回答是習慣性的。

如果沒有它,提交評論會發布之前提交的評論(總是落後一個)。

+0

就是這樣,我在發表評論時仍然收到錯誤,因爲現在我的first_name不匹配屬性,即使我已經在兩個數據庫上分配了auth.signature。 錯誤: ('NoneType'對象沒有屬性'first_name') –

0

當提交表單時,你是在之前選擇現有註釋處理表單(插入新的評論),因此新提交的評論將不會包含在頁面上顯示的評論中。只是扭轉最後兩行的順序:

form = SQLFORM(db.product_comments).process() 
    comments = db(db.product_comments.products==post.id).select() 

此外,你應該擺脫所有XML(..., sanitize=True)呼叫的,因爲他們是完全不必要的。這是爲了當您需要繞過模板中的默認轉義,但需要進行消毒時,因爲內容不可信。在這種情況下,您不需要繞過任何內容的轉義。

+0

我這樣做了,但現在我有一個('NoneType'對象沒有屬性'first_name')錯誤,即使我在兩個表中都有auth.signature屬性。謝謝你的回覆,並對我的慢回覆安東尼抱歉。 –

+0

是否可以在未登錄的情況下添加評論?如果是這樣,那麼這些註釋在'created_by'字段中就沒有價值。 – Anthony