2017-04-16 17 views
0

使用web.py編寫Python程序:蟒蛇web.py的模板HTML文件中的錯誤寫

class user_name_friend_sub: 
    def POST(self): 
     u_n = web.input() 
     u_name = u_n.user_name 
     u_id = u_n.user_id 

     user_friends_list = self.user_info_page(u_name,u_id) 
     return render.user_friends(user_friends_list) 

user_friends_list是包含許多DIC,就像一個清單:

[{'user_id': '1146214671', 'user_name': 'Xiaohong Xu', 'user_unit': 'Toronto, Ontario'}, 
... 
... 
{'user_id': '668347108', 'user_name': 'Lynn Zou', 'user_unit': 'Lead Software Engineer I at American Institutes for Research (AIR)'}] 

和模板HTML文件 - user_friends.html是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
    <title>Document</title> 
    <style> 
     h1 { 
      text-align: center; 
      font-size:30px; 
     } 
    </style> 
</head> 
<body> 
    <h1>User friends data</h1> 
    $def with (user_friends_list) 
    <ul> 
    $for user_friend in user_friends_list: 
     <li>$user_friend["user_name"]</li> 
    </ul> 
</body> 
</html> 

運行時,錯誤:

File "templates\user_friends.html", line 21 
def with (user_friends_list) 
    ^
SyntaxError: invalid syntax 

Template traceback: 
    File 'templates\\user_friends.html', line 21 
     </html> 

然而,當我刪除幾乎所有的HTML元素,在user_friends.html成爲:

$def with (user_friends_list) 
    <ul> 
    $for user_friend in user_friends_list: 
     <li>$user_friend["user_name"]</li> 
    </ul> 

它運行正常, 請你告訴我理由

回答

0

Python是一種服務器 - 方語言。這意味着它不能通過網頁直接執行,因爲它在服務器上運行(隱藏於用戶可見性之下)。您必須在.py文件中聲明python代碼。例如,您的HTML代碼可以將元素提交到cgi終點,然後允許python消化並返回值:www.tutorialspoint.com/python/python_cgi_programming.htm

+0

對不起,我不明白 – bin