简体   繁体   English

python web.py在模板html文件中写入错误

[英]python web.py write in template html file error

use web.py to write a python program: 使用web.py编写python程序:

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 is a list containing many dic,like: 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)'}]

and the template html file - user_friends.html is: 模板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>

when running,it errors: 运行时出现错误:

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>

however, when I delete almost all the html element, the user_friends.html become: 但是,当我删除几乎所有html元素时,user_friends.html变为:

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

it runs ok, could you please tell me the reason 运行正常,能否请您告诉我原因

Python is a server-side language. Python是一种服务器端语言。 This means it cannot be executed via a webpage directly, as it runs on a server (hidden from user visibility.) You must declare python code in .py files. 这意味着它不能直接通过网页执行,因为它在服务器上运行(对用户可见)。您必须在.py文件中声明python代码。 Your HTML code could submit elements to a cgi end point, for example, which then allows the python to digest and return values: www.tutorialspoint.com/python/python_cgi_programming.htm 例如,您的HTML代码可以将元素提交到cgi端点,然后该端点允许python消化并返回值:www.tutorialspoint.com/python/python_cgi_programming.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM