Skip to main content
 首页 » 编程设计

python之这个 ZPT 模板有什么问题

2025年05月04日8bluestorm

我正在开发的 Python 网站中使用 Chameleon。这是一段代码:

<option tal:repeat="option options" 
        value="${option.isoformat()}" 
        selected="${if request.get_param('%s%d%s' %(day, row, type))==option.isoformat(): 'selected'}"> 
    ${int(option.strftime('%I'))}${option.strftime(':%M%p')} 
</option> 

但出于某种原因,我总是遇到语法错误:

ExpressionError: invalid syntax 
 
- String:   "if request.get_param('%s%d%s' %(day, row, type))==option.isoformat(): 'selected'" 
- Filename: /Users/theron/Sites/python/restaurateur/views/settings/hours.pt 

我看不出我做错了什么。有帮助吗?

请您参考如下方法:

好吧,我想我明白了:它是无效的,因为没有 else 语句,所以当 if 语句评估为 false 时没有输出。我用以下方法修复了它:

<option tal:repeat="option options" 
        value="${option.isoformat()}" 
        tal:attributes="selected 'selected' if request.get_param('%s%d%s' %(day, row, 
        type))==option.isoformat() else None"> 
    ${int(option.strftime('%I'))}${option.strftime(':%M%p')} 
</option>